Monday, December 14, 2009

Copy a folder from one list to another list programmatically.

using (SPSite site = new SPSite("http://testserver/spsite"))
{
using (SPWeb web = site.OpenWeb())
{
string sourceListUrl = "CustomList1/";
string destinationListUrl = "CustomList2/";

SPFolder rootFolder = web.GetFolder(sourceListUrl);
foreach (SPFolder objRootSubFolder in rootFolder.SubFolders)
{
if (objRootSubFolder.Name != "Forms")
{
folder = web.GetFolder(objRootSubFolder.ServerRelativeUrl);
foreach (SPFolder objFolder in folder.SubFolders)
{
objFolder.CopyTo(destinationListUrl + objRootSubFolder.Name + "/" + objFolder.Name);
}

foreach (SPFile objFile in folder.Files)
{
objFile.CopyTo(destinationListUrl + objRootSubFolder.Name + "/" + objFile.Name);
}
}
}
}
}

Delete all site groups programmatically.

SPSecurity.RunWithElevatedPrivileges(delegate
{
using (SPSite spSite = new SPSite("http://testserver/spsite"))
{
using (SPWeb spWeb = spSite.OpenWeb())
{
SPGroupCollection objCol = spWeb.SiteGroups;

for (int count = 0; count <= objCol.Count - 1; count++)
{
spWeb.SiteGroups.Remove(0);
}
}
}
});

Delete all the items from a custom list or a document library.



SPSecurity.RunWithElevatedPrivileges(delegate
{
using (SPSite spSite = new SPSite("http://testserver/spsite"))
{
using (SPWeb spWeb = spSite.OpenWeb())
{
SPList objLst = spWeb.Lists["List Name"];
SPListItemCollection objCol = objLst.Items;

for (int count = 0; count <= objCol.Count - 1; count++) { objLst.Items.Delete(0); } } } });

Updating a list item by avoiding unnecessary call to event receiver.

If there is custom list and a event receiver[For creating/updating list item] is binded to that, then suppose we want to update the list without calling to the event receiver, then we have to disable the event firing and call the SystemUpdate() function instead of Update() function. Below here is the example in 2 steps:

Step 1:


class UpdateList: SPItemEventReceiver
{
}



Step 2:

SPListitem listItem = lst.Items.Add();
listItem["Title"] = "Samarendra Swain";
listItem["Company"] = "Manuh Solutions";

base.DisableEventFiring();
listItem.SystemUpdate();
base.EnableEventFiring();

Fetching SPList object properly.

When ever we need to access a Custom List or a Document Library , we normally write like:

SPList lst = web.Lists["List Name"];

But this is a performance killer. So avoid this and modify the code to:

SPList lst = web.GetList("http://testServer/Lists/ListName");

Proper Use of AllowUnsafeUpdates property.

Normally if we do not need to set the AllowUnsafeUpdates property to true in case of windows application. But if you are calling the SharePoint API in web based application then we need to set the AllowUnsafeUpdates property to true before creating/updating anything(e.g. creating/updating list item in a list).

Suppose there is assignment to create a list item and bind custom permission to that list item, then we normally write BL like:

web.AllowUnsafeUpdate = true;

SPLIstItem lstItem = list.Items.Add();
lstItem["Title"] = "Samarendra Swain";
lstItem.update();


Then with the return listitem object, we write BL to bind the role defination to the list item.

lstitem.BreakRoleInheritance(false);

SPRoleDefinitionCollection objWebRoleDefn = oSPWeb.RoleDefinitions;
SPRoleAssignment objRoleAssign = new SPRoleAssignment(objSPGroup);

objRoleAssign.RoleDefinitionBindings.Add(objWebRoleDefn["Full Control"]);
lstItem.RoleAssignments.Add(objRoleAssign);.


If you will run this, then you will get a run time error because of the AllowUnsafeUpdates property value set to false.

So,if you are writing lstitem.BreakRoleInheritance(false); then it makes the AllowUnsafeUpdates property value to false , so that after writing that line, you have to write again web.AllowUnsafeUpdate = true;

Then your code will run successfiully.

Tuesday, October 13, 2009

Getting QueryString Parameters with JavaScript in SharePoint

To get the query string parameter in a sharepoint page using javascript, below is the code snippet.

// call the EnsureSetup method
JSRequest.EnsureSetup();

// get the querystring parameter named empid e.g ...\test.aspx?empid=55&empname=sam
var empid = JSRequest.QueryString["empid"];
var empname = JSRequest.QueryString["empname"];

***********************************************************************
Output : empid = 55 and empname = Sam
***********************************************************************


Author: Samarendra Swain