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();
Monday, December 14, 2009
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");
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.
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
// 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
Wednesday, September 30, 2009
Display the matching results in People Picker Control by clicking on check names link
You will find a function EntityEditorCallback(result,ctx,preventAutoPostBack) in core.js file. At the last line of the function, just call this function: onClickRw(true,false);
what ever you write in People Picker Control, It will display the matching results by clicking on Check Names link Or by pressing enter key.
No need to again click on that text and see the matching results.
-samarendra swain
what ever you write in People Picker Control, It will display the matching results by clicking on Check Names link Or by pressing enter key.
No need to again click on that text and see the matching results.
-samarendra swain
Thursday, September 24, 2009
Delete a sharepoint group programmatically ?
To delete a sharepoint group using object model, below is the code snippet in c#.
// objSPWeb is SPWeb object
objSPWeb.SiteGroups.Remove(groupName);
objSPWeb.Update();
// objSPWeb is SPWeb object
objSPWeb.SiteGroups.Remove(groupName);
objSPWeb.Update();
Update the List item permission programmatically
Suppose if you have already bind a sharepoint group to a list item in a custom list/library for integrating permission then later on , if you want to modify the permission for that list item, below is the code snippet to achive it.
SPRoleAssignment objRoleAssignment = objListItem.RoleAssignments.GetAssignmentByPrincipal(objSPGroup);
objRoleAssignment.RoleDefinitionBindings.Remove(web.RoleDefinitions["Full Control"]);
objRoleAssignment.RoleDefinitionBindings.Add(web.RoleDefinitions["Contribute"]);
objRoleAssignment.Update();
Samarendra Swain
SPRoleAssignment objRoleAssignment = objListItem.RoleAssignments.GetAssignmentByPrincipal(objSPGroup);
objRoleAssignment.RoleDefinitionBindings.Remove(web.RoleDefinitions["Full Control"]);
objRoleAssignment.RoleDefinitionBindings.Add(web.RoleDefinitions["Contribute"]);
objRoleAssignment.Update();
Samarendra Swain
Subscribe to:
Posts (Atom)