18 March 2009

MOSS 2007 tip: RunWithElevatedPrivileges to update a list in SharePoint

Recently we had a requirement where we had to create a folder in a document library whenever a listitem was added in another list. Although this seemed simple and the code worked properly in development environment, we started getting errors when we had the application running in production. The reason was that a few users had add rights on one of the lists but had only view rights on the document library in which the folder had to be created.

SharePoint’s SPSecurity has a wonderful but dangerous (if not used properly) method which provides a solution to the above problem.

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spsecurity.runwithelevatedprivileges.aspx gives a detailed description of the method. Point to note here is that the SPSite or SPWeb object needs to be recreated to execute under full control rights. Remember to create the object with the C# using construct so that the object is disposed once the required task is completed.

No comments:

Post a Comment