Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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.

share|improve this question
    
I fear I already know the answer, but... the log doesn't contain any other info that provides more detail on the problem right? –  SPArchaeologist Jan 2 '13 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 :) –  Muhammad Raja Jan 2 '13 at 17:36

2 Answers 2

up vote 1 down vote accepted

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;
                        }
                    }
                }
        }
share|improve this answer
    
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. –  SPArchaeologist Jan 3 '13 at 8:06
    
Sorry that was more like a typing error, I turned off updates after updating list item. –  Muhammad Raja Jan 3 '13 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. –  SPArchaeologist Jan 3 '13 at 9:26

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;
    }
    }
});
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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