Monday, December 14, 2009

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";

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


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

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

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.

No comments:

Post a Comment