Donnerstag, 26. Januar 2012

Powershell script how to create a list

You might want to create a list with Powershell. Here is some code you can use (Replace the values in % with your values):

[void][System.reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
$siteUrl = %your site-url%
$site = new-object Microsoft.SharePoint.SPSite($siteUrl)
$web = $site.rootweb
$templateType = $web.ListTemplates["Custom List %or add another list-template%"]
$web.Lists.Add("%enter the list-name you want to use%", "%enter the description%", $templateType)
$web.Dispose()
$site.Dispose()

Freitag, 20. Januar 2012

Powershell script how to change a property of list(s) or page(s)

Sometimes you might have to change a property of a list or page in a site-collection. Maybe the amount of items is so large, that handling this manually would take ages to do.
Here is a script, where I change all the list-titles to add the " 1" to it.

$sites = Get-SPSite
foreach($site in $sites)
{
$web = $site.RootWeb
foreach($list in $web.lists)
{
$title = $list.Title
$list.Title = "$title 1"
$list.Update()
}
}


This code changed now the title of each list in your web-pplications to it's origin name plus the string " 1". You can also check for a specific site-collection to make the changes at:
$sites = Get-SPSite
foreach($site in $sites)
{
if($site.Url -eq "%url of the site-collection you want to make the changes in%")
{
$web = $site.RootWeb
foreach($list in $web.lists)
{
$title = $list.Title
$list.Title = "$title 1"
$list.Update()
}
}
}

Powershell script how to add a webpart to a page

This script in no. 1 will add a webpart to a single page, no 2 will iterate through a site-collection and add the webpart to each sub-site/page. Please change the values in the %%- marked areas according to your settings/environment. Best practice would be to copy the code into PowerGui or any power-shell editor to see the code structured and the comments highlighted.

1. Adding a webpart to a single SPWeb
#load the assembly of your webpart
[void][reflection.assembly]::LoadWithPartialName("%your webpart assembly-name%")

#this code will do the publishing of the file, if publishing/versioning is disabled, you will not need this code
function PublbishSPFile([Microsoft.SharePoint.SPFile] $spfile)
{
if ($spfile.Level -eq [Microsoft.SharePoint.SPFileLevel]::Checkout)
{
$spfile.CheckIn("%your comment%", [Microsoft.SharePoint.SPCheckInType]::MajorCheckin)
}
if ($page.ListItem.ParentList.EnableModeration)
{
if($page.ListItem.ModerationInformation.Status -eq [Microsoft.SharePoint.SPModerationStatusType]::Pending)
{
$spfile.Approve("%your comment%")
}
if($page.ListItem.ModerationInformation.Status -eq [Microsoft.SharePoint.SPModerationStatusType]::Draft)
{
$spfile.Publish("%your comment%")
}
}
}

#you have to get the web, where you want to add the webpart, you can also include everything into a function and call the function, when iterating, this is explained in no. 2
$site = Get-SPSite -Identity "%url of the site%"
$web = $site.RootWeb

#first you have to check out the file/page, if publishing/versioning is disabled, then you can skip this step
$file = $web.GetFile("/Pages/%your page%.aspx")
$file.CheckOut()
#initialize the webpart-manager and locate it to the page
$webpartmanager = $web.GetLimitedWebPartManager("%complete url%/Pages/%your page%.aspx", [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)

$webpart = new-object %add the object/class with namespace here, that corresponds to your webpart (e.g. Mynamespace.MyWebpartClass)%
$webpart.Title = "%title of the webpart%"
$webpart.ChromeType = [System.Web.UI.WebControls.WebParts.PartChromeType]::None;
#if your webpart does need some properties to be set, do it now
$webpartmanager.AddWebPart($webpart, "Zone1",0); #the code in the brakets adds the $webpart to the mentioned zone and sets the sorting of the webpart on the first place
$webpartmanager.Dispose();
#last but not least you have to check in the file/page, if publishing/versioning is disabled, then you can skip this step
PublbishSPFile($file)


2. Adding a webpart to multiple SPWebs
#load the assembly of your webpart
[void][reflection.assembly]::LoadWithPartialName("%your webpart assembly-name%")

#this code will do the publishing of the file, if publishing/versioning is disabled, you will not need this code
function PublbishSPFile([Microsoft.SharePoint.SPFile] $spfile)
{
if ($spfile.Level -eq [Microsoft.SharePoint.SPFileLevel]::Checkout)
{
$spfile.CheckIn("%your comment%", [Microsoft.SharePoint.SPCheckInType]::MajorCheckin)
}
if ($page.ListItem.ParentList.EnableModeration)
{
if($page.ListItem.ModerationInformation.Status -eq [Microsoft.SharePoint.SPModerationStatusType]::Pending)
{
$spfile.Approve("%your comment%")
}
if($page.ListItem.ModerationInformation.Status -eq [Microsoft.SharePoint.SPModerationStatusType]::Draft)
{
$spfile.Publish("%your comment%")
}
}
}

#I used the code from no 1 and enclosed it into a function, that will be called in an iteration
function AddWebpart([string] $web)
{
#first you have to check out the file/page, if publishing/versioning is disabled, then you can skip this step
$file = $web.GetFile("/Pages/%your page%.aspx")
$file.CheckOut()
#initialize the webpart-manager and locate it to the page
$webpartmanager = $web.GetLimitedWebPartManager("%complete url%/Pages/%your page%.aspx", [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)

$webpart = new-object %add the object/class with namespace here, that corresponds to your webpart (e.g. Mynamespace.MyWebpartClass)%
$webpart.Title = "%title of the webpart%"
$webpart.ChromeType = [System.Web.UI.WebControls.WebParts.PartChromeType]::None;
#if your webpart does need some properties to be set, do it now
$webpartmanager.AddWebPart($webpart, "Zone1",0); #the code in the brakets adds the $webpart to the mentioned zone and sets the sorting of the webpart on the first place
$webpartmanager.Dispose();
#last but not least you have to check in the file/page, if publishing/versioning is disabled, then you can skip this step
PublbishSPFile($file)
}

$site = Get-SPSite -Identity "[url of the site]"
foreach($subsite in $site.AllWebs)
{
AddWebpart $subsite
}

Powershell script how to change the master-page

If you want to change your master-page in your whole site-collection with all sub-sites, then you can use this powershell-script to manage this quite easily (change the %%-values according to your settings or needs):

$site = Get-SPSite -Identity "%url of the site%"
foreach($subsite in $site.AllWebs)
{
$subsite.AllowUnsafeUpdates = "True"
$subsite.CustomMasterUrl = "/_catalogs/masterpage/%my master-page%.master"
$subsite.Update()
$subsite.Dispose()
}

Donnerstag, 25. August 2011

Security Token Service not running

I wanted to start the profile synchronization job on my Sharepoint 2010 instance. One of the problems it did not work was, that the Security Token Service was not running.
I checked the health analyzer reports and there was the Security Token Service marked as not available. Then I went to IIS and there picked the SharePoint Web Services Site and in the tree below clicked SecurityTokenServiceApplication and checked the basic settings. when clicking "Test settings" I received an error, that the application path was not correct. Clicked the "Connect as" button and inserted a user, where I knew it has extended rights and voila the tested connection worked.

This was still not solving the issue with the profile synchronization, but at least one step closer.

Mittwoch, 13. Juli 2011

JQuery Ajax does not work after .NET Ajax request

I had the problem, that I was using an jquery ajax request on a website. I was using the document ready to initialize the function. When a .NET ajax request happend afterwards, the functionality of my jquery was not enabled anymore.

Solution of that was, to initialize the function to the Sys.WebForms.

It worked like that:

jQuery(document).ready(function () {

Myfunction();
load();
});

function load() {
Sys.WebForms.PageRequestManager.getInstance().add_endRequest(Myfunction);
}

Dienstag, 21. Juni 2011

Using temporary HttpContext in Sharepoint

Quite often you run into the problem, that classes do use the current HttpContext with all the permissions set. I had this problem e.g. with the PortalSiteMapProvider. It uses the regular HttpContext and there is no chance to set other properties to it. In this case you can use a temporary HttpContext to give it the SPWeb you need with the permissions of a specified user. After running your code you can turn back to the regular context.

public class TemporaryHttpContext : IDisposable
{
public TemporaryHttpContext()
{
thecurrentContext = HttpContext.Current;
HttpContext.Current = null;
}

public TemporaryHttpContext(SPWeb spweb, string userlogin) : this()
{
TemporyContext(spweb, userlogin);
}

private HttpContext thecurrentContext { get; set; }

public void TemporaryContext(SPWeb spweb, string userlogin)
{
if (spweb == null || !spweb.Exists)
{
throw new ArgumentNullException("web");
}

var request = new HttpRequest("", spweb.Url, "");
HttpContext.Current = new HttpContext(request, new HttpResponse(new StringWriter(CultureInfo.CurrentCulture)));

HttpContext.Current.Items["HttpHandlerSPWeb"] = spweb;

var wi = WindowsIdentity.GetCurrent();
if (wi != null)
{
FieldInfo fieldInfo = typeof(WindowsIdentity).GetField("m_name", BindingFlags.NonPublic | BindingFlags.Instance);
if (fieldInfo != null)
{
fieldInfo.SetValue(wi, userlogin);
}

HttpContext.Current.User = new GenericPrincipal(wi, new string[0]);
}
}

public void OldContext()
{
HttpContext.Current = thecurrentContext;
}

public void Dispose()
{
OldContext();
}
}


Afterwards just wrap your code into the temporary context like this:


TemporaryHttpContext tempcontext = new TemporaryHttpContext();
tempcontext.TemporaryContext(spweb, userloginname);
[Your code that needs a temporary context]
tempcontext.Dispose();

and then everything should work smoothly.

Dienstag, 7. Juni 2011

Webpart maintenance page

If you are running into the problem, that you added a webpart to your page and you want to remove it, because it breaks your page, then just add the query-string ?contents=1 to your URL and it will redirect you to the webpart maintenance page.

Webpart maintenance page

If you are running into the problem, that you added a webpart to your page and you want to remove it, because it breaks your page, then just add the query-string ?contents=1 to your URL and it will redirect you to the webpart maintenance page.

Mittwoch, 18. Mai 2011

Cannot generate serialization assembly ... Use /force to force an overwrite

I received this error when trying to deploy something to Sharepoint. The issue seems to be, that the dll is locked by some procedure of the IIS. Just perform in the command line a "iisreset" and try again.
Sometimes you also have to close Visual Studio and re-open after the iisreset and try again.