Tuesday, September 2, 2014

Custom SharePoint file replacing web service

SharePoint built-in web services do not support replacing files in document libraries. I had an integration project where metadata was sent with dummy file and updated later with the right document. I managed to do everything with SharePoint native web services except updating file and it's extension. So i created a custom web service for that purpose.

I will not cover how to create SharePoint custom web service, you can read about it here Walkthrough: Creating a Custom ASP.NET (ASMX) Web Service in SharePoint 2010

Code for the web service is:

   public class Service : System.Web.Services.WebService  
   {  
     public Service()  
     {  
       //Uncomment the following line if using designed components  
       //InitializeComponent();  
     }  
     [WebMethod]  
     public string ReplaceFile(string SiteUrl, string LibName, int ItemId, string NewDocName, byte[] FileByteArray)  
     {  
       string ReturnVal = "File uploaded and renamed. OK!";  
       try  
       {  
         SPSecurity.RunWithElevatedPrivileges(delegate()  
         {  
           SPSite site = new SPSite(SiteUrl);  
           SPWeb web = site.OpenWeb();  
           web.AllowUnsafeUpdates = true;  
           SPList list = web.Lists[LibName];  
           SPItem item = list.GetItemById(ItemId);  
           string oldFileUrl = item["EncodedAbsUrl"].ToString();  
           SPFile file = web.GetFile(oldFileUrl);  
           item["Title"] = NewDocName;  
           item.Update();  
           if (file.CheckOutType != SPFile.SPCheckOutType.None)  
           {  
             file.UndoCheckOut();  
           }  
           file.CheckOut();  
           file.SaveBinary(FileByteArray);  
           file.MoveTo(SiteUrl + item["FileDirRef"].ToString() + "/" + NewDocName, true);  
           file.Update();  
           file.CheckIn("");  
           web.Dispose();  
         });  
       }  
       catch (Exception ex)  
       {  
         return ex.Message.ToString(); ;  
       }  
       return ReturnVal;  
     }  
   }  

The code runs under elevated privileges, so every user can execute this regardless their permissions. If your solution requires that users could only do this on documents they have permissions for, then you should remove the delegation part. It also checks if the file is checked out and if is, discards it.

So if i depoly this web service i can access it from http://yourserver:80/_ReplaceFileWS/ReplaceFile.asmx
There i can see that i have one method named ReplaceFile and if i click on it i can see my web service request and response examples.


For request i must know the url of a SharePoint site where my replaceable file is located at (in this example it is http://yourserver:80), document library display name where the replaceable file is in, SharePoint ID of the replaceable file, name for the new document with file extension and the new document itself as byte array encoded in base64.
For response we get a string saying "File uploaded and renamed. OK!" if everything was correct or the exception message if there were some problems.

You can test this solution with a great tool named SoapUI. Here are instructions how Using SoapUI to test SharePoint web services

Souce: http://bit.ly/Y8bwNt
.wsp package: http://bit.ly/W5sN8z

No comments:

Post a Comment