3

So I have this really simple function which updates a couple of fields on two different lists (one is a document library) The thing is on the document library does not updates any of the fields I need to update.

Here's the code

public void Approval(int id, string comment, bool result)
        {

            string ownerLibraryName = string.Empty;
            string targetUserName = string.Empty;
            int targetId = 0;
            string userName = string.Empty;

            userName = SPContext.Current.Web.CurrentUser.Name;
            string userLibraryName = SPContext.Current.Web.CurrentUser.LoginName.GetUserWithOutDomain();


            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (SPSite noSecuritySite = new SPSite(SPContext.Current.Site.Url))
                {
                    noSecuritySite.AllowUnsafeUpdates = true;
                    using (SPWeb noSecurityWeb = noSecuritySite.OpenWeb())
                    {
                        noSecuritySite.AllowUnsafeUpdates = true;
                        SPList userLibrary = noSecurityWeb.Lists.TryGetList(userLibraryName);
                        if (userLibrary == null) { throw new Exception(string.Format("La biblioteca {0} no existe", userLibraryName)); }

                        SPListItem voboRequest = userLibrary.GetItemById(id);

                        targetId = int.Parse((voboRequest[CustomSiteColumns.FIELD_VOBO_ID].ToString()));
                        SPFieldUserValue docOwner = new SPFieldUserValue(voboRequest.Web, voboRequest[CustomSiteColumns.FIELD_VOBO_DOC_OWNER].ToString());
                        ownerLibraryName = docOwner.User.LoginName.GetUserWithOutDomain();    

                        voboRequest[CustomSiteColumns.FIELD_CURRENT_ACTION] = "MoveTo";


                        noSecurityWeb.AllowUnsafeUpdates = true;
                        SPList docOwnerLibrary = noSecurityWeb.Lists.TryGetList(ownerLibraryName);
                        if (docOwnerLibrary == null) { throw new Exception(string.Format("La biblioteca {0} no existe", ownerLibraryName)); }

                        SPListItem targetDoc = docOwnerLibrary.GetItemById(targetId);                        
                        targetDoc[CustomSiteColumns.FIELD_ARCHIVE_RESPONSE] = comment;
                        targetDoc[CustomSiteColumns.FIELD_ARCHIVE_APPROVAL] = result;

                        targetDoc.Log(string.Empty, DateTime.Now, result ? Support.Enums.TraceableOperation.Aprobado : Support.Enums.TraceableOperation.Rechazado, userName, comment);
                        targetDoc.Update();

                        voboRequest.Update();
                        voboRequest.Delete();
                        noSecurityWeb.AllowUnsafeUpdates = false;
                    }
                    noSecuritySite.AllowUnsafeUpdates = false;
                }
            });
        }

If I check the targetDoc[CustomSiteColumns.FIELD_ARCHIVE_RESPONSE] field after the targetDoc.Update(), I get a null value, but if I check the voboRequest[CustomSiteColumns.FIELD_CURRENT_ACTION] value after the voboRequest.Update() I get a 'MoveTo', which is precisely the value I just assigned.

Any ideas what might be happening?

  • I hesitate to post this as an answer because I can't provide a lot of evidence to back it up, but perhaps you need an SPList.Update() in there too. – thanby Feb 18 '13 at 16:23
  • Its kinda strange because it was working before. I just deleted the site collection and I'm re-running the SharePoint config wizard. I'll try it out when I finish the wizard and the site re-deployment – Jaime Feb 18 '13 at 16:27
  • i have the same error, how do yor resolver this problem? – user17542 Jun 10 '13 at 20:40
  • Where/When are you calling your code exactly? Try it on a txt file please. If it is working there you might have issues with property promotion/demotion for office files. – Heiko Hatzfeld_MSFT Mar 20 '18 at 13:16
1

Try updating the file in addition to the SPListItem:

targetDoc.Update()
targetDoc.File.Update()
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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