Microsoft Technology, .Net, BizTalk, Sharepoint & etc.

Liedong(Ken) Zheng, Senior SharePoint Developer at SIMPLOT

Archive for April 3rd, 2009

MOSS: SPListItem.Update() throws error Operation is not valid due to the current state of the object.

Posted by ken zheng on April 3, 2009

Problem:
When running SPListItem.Update commands inside SPSecurity.RunWithElevatedPrivileges block you get error: Operation is not valid due to the current state of the object.

Thoughts:
I will call this a workaround since I don’t know why it works and is this really how it should be done.

Workaround:
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, like this:

    private static string CreateSiteLink(SPWeb newSite, int groupId, string text)
    {
        string retVal = "";
        SPWeb elevatedRootWeb = null;

 try
  {
         SPSecurity.RunWithElevatedPrivileges(delegate()
         {
             using (SPSite elevatedSite = new SPSite(newSite.Site.ID))
             {
                 elevatedRootWeb = elevatedSite.RootWeb;
             }
         });

         SPList userInformation = elevatedRootWeb.Lists["User Information List"];

         if (userInformation != null)
         {
             try
             {
                 elevatedRootWeb.AllowUnsafeUpdates = true;
                 SPListItem item = userInformation.GetItemById(groupId); 

                 if (item["Notes"] != null)
                 {
                     item["Notes"] = string.Format(text, newSite.ServerRelativeUrl, newSite.Name);
                     item.Update();
                 }
                 elevatedRootWeb.AllowUnsafeUpdates = false;
             }
             catch (Exception e)
             {
                 retVal = " Site link for group " + groupId + " couldn't be created. (" + e.Message + ")";

             }
         }
         else
         {
             retVal = " Site link for group " + groupId + " couldn't be created. (User Information List not found)";
         }
 finally
 {
  elevatedRootWeb.Dispose();
 }

        return retVal;

    }

Posted in Sharepoint | Tagged: | 3 Comments »

Get Count from SharePoint List for InfoPath

Posted by ken zheng on April 3, 2009

I always be asked to get a Numberic Id for new infopath form. Instead of writing code in Form, I created a Web Service which will go to the SharePoint List, grab the number and increment by 1, then update the list. By doing that, you can set rule in your Form Submit Button without code required.
Below is the code

[WebMethod]
        public int GetAndIncrementCount(string ListUrl, string CountName)
        {
            int count = 0;

            SPWeb oSPWeb = null;
            try
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    using (SPSite oSPSite = new SPSite(ListUrl))
                    {
                        oSPWeb = oSPSite.OpenWeb(ListUrl.Substring(ListUrl.IndexOf("/") + 1));
                    }
                });
                SPList oSPList = oSPWeb.GetList(ListUrl);

                if (oSPList != null)
                {
                    oSPWeb.AllowUnsafeUpdates = true;
                    SPListItemCollection items = oSPList.Items;
                    if (items.Count > 0)
                    {
                        SPListItem item = items[0];
                        count = int.Parse(item[CountName].ToString()) + 1;
                        item[CountName] = count;
                        item.Update();
                    }
                }
            }
            catch (Exception exception)
            {
                Log.WriteLogEvent(string.Format("An Error Occured In GetAndIncrementCount Method | Exception Message:{0} StackTrace: {1}", exception.Message, exception.StackTrace));
            }
            finally
            {
                oSPWeb.Dispose();
            }
            return count;
        }

Posted in Sharepoint | Tagged: , | Leave a Comment »