Prasad Bolla's SharePoint Blog

Click Here to go through the Interesting posts within my Blog.

Click Here to go through the new posts in my blog.

Thursday, December 01, 2011

Insertion into SharePoint List Using SpSecurity Priviledges Code.

Note:
When you create a splistitem using SpSecurity Privileges code with person who is having non admin permission then the listitems will be created with system account.
SPWeb currentWeb = SPContext.Current.Web;
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site = new SPSite(currentWeb.Url))
{
using (SPWeb web = site.OpenWeb())
{
web.AllowUnsafeUpdates = true;
SPListItemCollection mycoll = web.Lists["Login"].Items;
SPListItem item = mycoll.Add();
item["Title"] = txtUserName.Text;
item["Password"] = txtPassWord.Text;
item.Update();
txtUserName.Text = "";
txtPassWord.Text = "";
strError += "Record Saved Succesfully";
}
}
});

No comments:

Post a Comment