Quantcast
Channel: SharePoint 2013 - Development and Programming forum
Viewing all articles
Browse latest Browse all 7589

How to robustly change document file name in a SharePoint 2013 library?

$
0
0

Hi all,

Internet is full with examples of renaming SharePoint files like this code:

         

         string documentId = (properties.ListItem["_dlc_DocId"] ?? string.Empty).ToString();
            SPSecurity.RunWithElevatedPrivileges(delegate
            {
                using (SPSite site = new SPSite(properties.Site.ID))
                {
                    using (SPWeb web = site.OpenWeb(properties.Web.ID))
                    {
                            SPListItem item = web.Lists[properties.ListItem.ParentList.Title].GetItemById(properties.ListItem.ID);
                            item[Fields.SPPDDocumentId.ID] = documentId;
                            item.SystemUpdate(false); // if error later.
                            item.File.CheckOut();
                            item["Name"] = documentId;
                            item.SystemUpdate(false);
                            item.File.CheckIn("Renamed");
                      } // inner using
                } // outter using
            }); // SPSecurity.RunWithElevatedPrivileges

We have had much problem with simple code like the one above in ItemCheckedIn. The code is not robust at all. It seems that that there must be a test on

1 item.File.CheckOut()

2 item["Name”]

3 item.File.CheckIn()

4 item.SystemUpdate

to ensure that they are finished correctly before next step.

It seems like on the first three, I must make (slightly different) tests in a for loop after each statement to test finished correctly. if I don’t know about item.SystemUpdate – is there a way to test that it is executed correctly?

Has anyone else these problem and are they solve as outlined or is there a more clever tests. The outlined for loop solutions are many rows for each test and two of them contains a Try Catch constructs?

Of course then, when debug code is removed, every time in ItemCheckedOut I have problems with error like. The file <file name> has been modified <user and time>. Whit debug code I rarely have that the error. I assume I must put in some magic test there also on file locks.

For me SharePoint feels more an more  like an unfinished toy for a serious ECM-solutions.

Best Regards

Csaba Urbaniczky



Viewing all articles
Browse latest Browse all 7589

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>