Problems with updating list items in a document library

Soru Problems with updating list items in a document library

  • 05 Mayıs 2012 Cumartesi 23:53
     
      Kod İçerir

    Here is my setup:

    SharePoint Installation: SharePoint Server 2010

    Problem:

    I have a document library that have two additional fields a lookup (which points to a custom sharepoint list) and a text field column. I've written a console application that will on the document library and capture the selected lookup value and copy this value to the text field column for each list item.

    Below is my implementation:

     string siteUrl = "http://<ComputerName>";
                string docLibName = "Documents";
                string lupColumnName = "LegalEntityLup";
                string txtColumnName = "LegalEntityText";
                using (SPSite spSite = new SPSite(siteUrl))
                {
                    using(SPWeb spWeb = spSite.RootWeb)
                    {
                        SPList spList = spWeb.Lists.TryGetList(docLibName);
                        if (spList != null)
                        {
                            SPDocumentLibrary docLib = spList as SPDocumentLibrary;
                            SPListItemCollection listItemCollection = docLib.Items;
                            foreach (SPListItem listItem in listItemCollection)
                            {
                                string fieldValue = listItem[lupColumnName].ToString();
                                string beforeValue = (listItem[txtColumnName] == null) ? "" : listItem[txtColumnName].ToString();
                                SPFieldLookupValue fieldLookupValue = new SPFieldLookupValue(fieldValue);
                                listItem[txtColumnName] = fieldLookupValue.LookupValue;
                                string afterValue = listItem[txtColumnName].ToString();
                                listItem.SystemUpdate(true);
                                Console.WriteLine(string.Format("Doc Name {0} - Before {1} - After {2}",listItem["Name"].ToString(), beforeValue, afterValue));
                            }
                            Console.Read();
                        }
                    }

    Make sure you mark my reply as the answer if it had solved your request. Brandon M. Hunter MCTS - SharePoint 2010 Configuration

Tüm Yanıtlar

  • 05 Mayıs 2012 Cumartesi 23:54
     
     

    Whenever I run my code, all of the selected lookup values is copied to the text field column on each list item except the last one. For some reason, the last item to be update is never updated.

    Below is a screenshot of the results after the console application completed execution. This screenshots shows you the before and after value of the text field column. Note that Doc7.docx shows the before value as 'Item 1' and the after value as  'Item 2'.

    Below is a screenshot of the results from the document library. Notice here Doc7, under the LegalEntityText value is Item 1, where it should be Item 2.


    Make sure you mark my reply as the answer if it had solved your request. Brandon M. Hunter MCTS - SharePoint 2010 Configuration

  • 05 Mayıs 2012 Cumartesi 23:55
     
     

    Looking at the version history for Doc7, it shows that the text field column updated to 'Item 2' on version 6.0, but it also said that a new version was created (7.0) where it sets the text field column back to 'Item 1'.

    Help is needed. I've been banging my head for days on this.

    Trying to call the update method on the SPDocumentLibrary.Update method works, but throws an exception because  of a editing conflict.

    SORRY I HAD TO REPLY TO MY SAME THREAD THREE TIMES BECAUSE I HAVE MORE THAN TWO IMAGES FOR A THREAD.


    Make sure you mark my reply as the answer if it had solved your request. Brandon M. Hunter MCTS - SharePoint 2010 Configuration

  • 06 Mayıs 2012 Pazar 03:37
     
     

    I'm wondering if somehow that document is checked out. Have you seen this: http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.splistitem.systemupdate.aspx See the Community content section at the bottom. It advocates using AllowUnsafeUpdates and make the Boolean on SystemUpdate false instead of true.


    Doug Hemminger http://www.sharepointdoug.com

  • 06 Mayıs 2012 Pazar 07:14
    Yanıtlayıcı
     
     

    Hello,

    Have you some event handler modifing the item on the document library?

    the editing conflict make me think to that.


    Best regards, Christopher.
    Blog | Mail

  • 15 Mayıs 2012 Salı 00:14
     
     
    Even making the changes that you suggested I still get the same results.

    Make sure you mark my reply as the answer if it had solved your request. Brandon M. Hunter MCTS - SharePoint 2010 Configuration

  • 15 Mayıs 2012 Salı 00:14
     
     
    I do not have an event handler nor a workflow that modifies any document library in my site.

    Make sure you mark my reply as the answer if it had solved your request. Brandon M. Hunter MCTS - SharePoint 2010 Configuration