2

I am trying to update a list item's custom property in asp.net code behind file but getting Error 500 without any explanation or further error, while debugging I realized it happens while calling update method for item, I tried same code in Console application for same list and it worked perfectly. Here's the code,

    List<string> someStringList = new List<string>();

    foreach(string sD in someString)
        someStringList.Add(sD);

    SPSecurity.RunWithElevatedPrivileges(delegate
    {
        using (SPSite site = new SPSite(webURL))
        using (SPWeb web = site.OpenWeb())
        {
            web.AllowUnsafeUpdates = true;

            int items = web.Lists[listName].ItemCount;

                foreach (SPListItem listItem in web.Lists[listName].Items)
                {
                    if (listItem != null && !someStringList.Contains(listItem.Name))
                    {
                        listItem.BreakRoleInheritance(true);// = "ItWorksButNot";
                        // listItem.Update();

                        listItem["Is Document Important"] = "True";

                        listItem.Update();
                        //listItem.SystemUpdate(false);
                        //web.Lists[listName].Update();
                    }
                }

            web.AllowUnsafeUpdates = false;
        }
    });

Now custom column Name is fine and it does exists as I can check it while debugging.

2
  • I fear I already know the answer, but... the log doesn't contain any other info that provides more detail on the problem right?
    – SPArcheon
    Jan 2, 2013 at 13:21
  • Nope not really, but I made it work now somehow not sure how tho as I didn't really changed any code, thanks for looking at it tho :) Jan 2, 2013 at 17:36

2 Answers 2

1

try these block of code..it may solve your problem..

    List<string> someStringList = new List<string>();

foreach(string sD in someString)
    someStringList.Add(sD);
SPSite csite=SPContext.Current.Site;
SPSecurity.RunWithElevatedPrivileges(delegate
{

    using (SPSite site = new SPSite(csite.ID,csite.SystemAccount.UserToken))
    {
    using (SPWeb web = site.OpenWeb())
    {
        web.AllowUnsafeUpdates = true;

        int items = web.Lists[listName].ItemCount;

            foreach (SPListItem listItem in web.Lists[listName].Items)
            {
                if (listItem != null && !someStringList.Contains(listItem.Name))
                {
                    listItem.BreakRoleInheritance(true);// = "ItWorksButNot";
                    // listItem.Update();

                    listItem["Is Document Important"] = "True";

                    listItem.Update();
                    //listItem.SystemUpdate(false);
                    //web.Lists[listName].Update();
                }
            }

        web.AllowUnsafeUpdates = false;
    }
    }
});
1

I didn't really changed much in code but it started working now, I don't really know what was the cause of error 500 either, however changes I made to code includes following,

  List<string> someStringList = new List<string>();
// removing elevated privileges didn't really changed anything but I removed it as 
// I know user will have permissions for updating list item.
    foreach(string sD in someString)
        someStringList.Add(sD);

        using (SPSite site = new SPSite(webURL))
        using (SPWeb web = site.OpenWeb())
        {
            int items = web.Lists[listName].ItemCount;

                foreach (SPListItem listItem in web.Lists[listName].Items)
                {
                    if (listItem != null)
                    {
                        if(someStringList.Contains(listItem.Name))
                             Continue;
                        else
                        {
                            web.AllowUnsafeUpdates = true;
                            listItem["Is Document Important"] = "True";
                            listItem.Update();
                            web.AllowUnsafeUpdates = false;
                        }
                    }
                }
        }
3
  • Some operations seems to reset the "AllowUnsafeUpdate" flag - maybe that was the problem. If you have some spare time, I would have a go with the original code you posted in the question and see if just before the call to update web.AllowUnsafeUpdates is still true.
    – SPArcheon
    Jan 3, 2013 at 8:06
  • Sorry that was more like a typing error, I turned off updates after updating list item. Jan 3, 2013 at 8:42
  • Hehe, didn't even notice that. What I was suggesting is that in my experience sometimes the AllowUnsafeUpdates is reset to false by some internal operation - so it is better to set it just before the problematic step. I was wondering if that could be your case - basically I was asking myself if in your original code the flag was still set when .Update() is called, or in between SharePoint decided to reset it.
    – SPArcheon
    Jan 3, 2013 at 9:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.