Thursday, December 17, 2009

Custom EnsureUser function

The functionality of web.EnsureUser("login name") function is to resolve the user from the sharepoint site. But there might be a case where the login name you are passsing as a parameter, which does not exist in the site, then it will give you a run time error.

But this function will work like a brilliant. It will check the user and if does not exist then add that to "Home Owners" group. You can specify your own group name also.

public SPUser CustomEnsureUser(string fullName,SPWeb web)
{
SPPrincipalInfo objInfo = SPUtility.ResolvePrincipal(web, fullName, SPPrincipalType.SecurityGroup | SPPrincipalType.User, SPPrincipalSource.All, null, false);
if objInfo == null)
{
throw new SPException(SPResource.GetString("User could not be found", new object[]{objInfo.LoginName}));

}
if (objInfo.PrincipalId < 0) { web.Groups["Home Owners"].AddUser(objInfo.LoginName, objInfo.Email, objInfo.DisplayName, string.Empty); } return web.SiteUsers[objInfo.LoginName]; }

Wednesday, December 16, 2009

How to open SharePoint Group easily in Out-Of-Box functionality.

Suppose you are creating a application ,where you are creating sharepoint custom groups day by day. After some days, suppose there are 5000 custom sharepoint groups and you need to Modify settings/update users/delete users in a specific group. Then how can you go to that group easily ?

Step 1.Where ever you will see that group,just right click on that and click properties and get address url like:

http://testserver/testsite/_layouts/editprms.aspx?
obj=%7B9FD2AD57%2DB7E1%2D4C7C%2D9A7C%2DA1BB85B20AEC%7D%2C614%2CLISTITEM
&sel=17637

Step 2: Take the sel value(i.e 17637) and Type the url as the below example:
http://testserver/testsite/_layouts/people.aspx?MembershipGroupId=17637

Then you will reach that Group direcltly.

Try it out and enjoy !

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.