Problem : How to insert / update a list item without write permission
You can use this code
SPSecurity.RunWithElevatedPrevileges(delegate(){
//place your code here
});
Your code above will be executed using admin rights. So you can insert / update a list item even though current login doesn't have permission to insert / update.
Note:
Remind that you cannot call method Update of SPListItem class inside the code above. It will throw error like this : "Operation is not valid due to the current state of the object".
I have googled about this problem and found a way to solve it, thanks to this great post.
The point is :You must not call the SPListItem.Update inside the RunWithElevatedPrivileges block. Instead you should only instantiate the SPSite or SPWeb there and call Update afterwards.
Here is the sample code :
SPSite site = new SPSite("http://moss.denallix.com");
SPWeb elevatedRootWeb = null;
SPWeb web = site.OpenWeb();
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite elevatedSite = new SPSite(web.Site.ID))
{
elevatedRootWeb = elevatedSite.RootWeb;
}
});
SPList myList = elevatedRootWeb.Lists["MyList"];
try
{
elevatedRootWeb.AllowUnsafeUpdates = true;
SPListItem myNewItem = myList.Items.Add();
myNewItem["Column1"] = "Column1Value";
myNewItem["Column2"] = "Column2Value";
myNewItem["Column3"] = "Column3Value";
myNewItem.Update();
}
catch (Exception ex)
{
EventLog.WriteEntry("Error", ex.Message.ToString(), EventLogEntryType.Error);
}
MOSS 2007 Tips : Dynamic Webpart Installer Class
Usually when we deploy a web part to MOSS , we have to develop 2 times. One for the installer class , one for the user control (ASCX) itself. A friend of mine has made a dynamic installer class so that we just have to set the ASCX path every time we want to make a web part. Here is the code.
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
namespace DynamicUserControlWebPart
{
public class DynamicUserControlWebPart : WebPart
{
private string _usercontrolvirtualpath;
private string ErrorMessage;
Control _control = null;
[WebBrowsable(true)]
[Personalizable(true)]
[WebDescription("Dynamic User Control WebPart")]
[WebDisplayName("Dynamic User Control WebPart")]
public string UserControlVirtualPath
{
get { return _usercontrolvirtualpath; }
set { _usercontrolvirtualpath = value; }
}
protected override void CreateChildControls()
{
this.Controls.Clear();
try
{
_control = this.Page.LoadControl(_usercontrolvirtualpath);
this.Controls.Add(_control);
}
catch (System.Exception ex)
{
ErrorMessage = ex.Message;
}
}
protected override void RenderChildren(HtmlTextWriter writer)
{
if (_control != null)
{
_control.RenderControl(writer);
}
else
{
writer.Write(ErrorMessage);
}
}
}
}
- Deploy this Web Part.
- Place your *.ASCX and *.ASCX.CS files in the moss folder (ex: C:\Inetpub\wwwroot\wss\VirtualDirectories\81\MyUserControl ).
- After adding this webpart to a page , just click Edit - Modify Shared Web Part.
- And then set the ASCX path to the property in the right column , Miscellaneous category. (ex : ~/MyUserControl/myUC.ascx )
- See the result.