views:

56

answers:

1

We have the following code in a webpart:

using (SPSite site = new SPSite("http://localhost/"))
{
  using (SPWeb web = site.OpenWeb())
  {
    SPList list = web.SiteUserInfoList;
    if (!list.Fields.ContainsField("Office"))
    {
        list.Fields.Add("Office", SPFieldType.Text, false);
        list.Update();
    }
  }
}

in the 4th line where we add a item to the list we get this error: "Updates are currently disallowed on GET requests. To allow updates on a GET, set the 'AllowUnsafeUpdates' property on SPWeb".

Then we added this line before adding the item to the list:

web.AllowUnsafeUpdates = true;

Now we are getting this error: "You are currently signed in as: [domain]\username". Sign in as a different user.

The account that we are using have administrative access too. Any idea how we can get the following code executing:

web.AllowUnsafeUpdates = true;
list.Fields.Add("Office", SPFieldType.Text, false);
list.Update();
A: 

Got it working the following way:

SPSecurity.RunWithElevatedPrivileges(delegate()
{
    using (SPSite site = new SPSite(web.Site.ID))
    {
      using (SPWeb web = site.OpenWeb())
      {
        SPList list = web.SiteUserInfoList;
        if (!list.Fields.ContainsField("Office"))
        {
            list.Fields.Add("Office", SPFieldType.Text, false);
            list.Update();
        }
      }
    }
});

Needed to run the codes with Full Control rights :) But we shouldn't be writings all the cods this way.

Rahat