Monthly Archives: May 2013

C# Extension to Delete selected SharePoint List Items

It is a common requirement to delete set of SharePoint list items from a list. In most case you might want to do that quickly as well. Here is a extension method that I wrote to accomplish that.

 public static void DeleteItemsCollection(this SPList list, SPWeb web, SPListItemCollection listitemCollection)
{
StringBuilder deletebuilder = DeleteBatchCommand(list, listitemCollection);
web.ProcessBatchData(deletebuilder.ToString());
}

 

StringBuilder DeleteBatchCommand(SPList spList, SPListItemCollection listitemCollection)
{
StringBuilder deletebuilder = new StringBuilder();
deletebuilder.Append(“<?xml version=\”1.0\” encoding=\”UTF-8\”?><Batch>”);
string command = “<Method><SetList Scope=\”Request\”>” + spList.ID +
“</SetList><SetVar Name=\”ID\”>{0}</SetVar><SetVar Name=\”Cmd\”>Delete</SetVar></Method>”;

foreach (SPListItem item in listitemCollection)
{
deletebuilder.Append(string.Format(command, item.ID.ToString()));
}
deletebuilder.Append(“</Batch>”);
return deletebuilder;
}

The method “DeleteItemsCollection” can be called up on any list together with SPWeb and the collection list items that you want to get rid of the list. You can simply execute the method as follows.

myList.DeleteItemsCollection(web, itemsToDelete);

 

Advertisements

Add Quick Launch Navigation Entries together with Localization Text using Powershell

Its a time where most SharePoint developers look to do everything possible with Powershell rather than writing thousands of C# code. One common simple such requirement is to add Navigation Entries to Quick Launch. This is specially very useful when you work with a site where publishing features are not available so that you can not use the GUI to add navigation entries either.

Here is the code,

    $webURL =  “http://mySPSiteURL/&#8221;
$enTitle = “Chaminda’s Blog ”
$frTitle = “Blog de Chaminda”
$urlLink =”https://chamindasomathilaka.wordpress.com&#8221;

$webs = Get-SPWeb -Identity $webURL -ea Stop

$navigation= $webs.Navigation.QuickLaunch

$culture = New-Object System.Globalization.Cultureinfo(“fr-FR”)
$cultureFrCA = New-Object System.Globalization.Cultureinfo(“fr-CA”)

$newnode = New-Object Microsoft.SharePoint.Navigation.SPNavigationNode($enTitle, $urlLink, $true)

$addedNode = $navigation.AddAsLast($newnode)

$addedNode.TitleResource.SetValueForUICulture($culture, $frTitle)
$addedNode.TitleResource.SetValueForUICulture($cultureFrCA, $frTitle)

$addedNode.Update()

$webs.Update()

That is to add a Quick Launch navigation entry directing my blog with both English and French translation (this is in case you have both English and French languages are available on your site).

Same code can be used to add Navigation entry to Top Navigation Area as well. you just need to initialize $navigation variable with correct navigation.

   $navigation= $webs.Navigation.TopNavigationBar