locked
Copy files from 1 Doc Lib to another RRS feed

  • Question

  • Hello,

    I have a small requirement where i want to mirror a Document Library, like backup. This means that whenever a file is added or deleted, the workflow should also add or delete the respective file in the target document library.

    The challenge here is my target document library is in different site collection which is in different farm. So i was unable to use Workflow and not even the Event Handlers where in we can use SOM (Sharepoint Object Model)

    Any views are welcome

    Thanks in Advance


    Monday, June 20, 2011 12:35 PM

Answers

  • Hi Surendar,

    oElement.SetAttribute("RootFolder", String.Format("/{0}/{1}", sDocLibName, sRootFolderName))

    this is working like a charm... i tested it in my environment within the same farm... can you try it again

    Regards, Rishi

    • Marked as answer by David HM Tuesday, June 28, 2011 1:19 AM
    Tuesday, June 21, 2011 7:36 AM
  • I have figured out why the above code was not working for me.

     

    oElement.SetAttribute("RootFolder", String.Format("/{0}/{1}", sDocLibName, sRootFolderName))
    


     In the above marked part while specifying the RootFolder I have given “/DocLib/RootFolder”, this will work if the site is a main site collection (as is was in your case)
    Ex. https://somedomain.com


    But my URL was https://somedomain.com/sites/sitename, if the URL is of this kind then RootFolder should be “/sites/sitename/doclibname/RootFolder”.

    So if the URL is of type https://somedomain.com/sites/sitename, the RootFolder should be

     

    oElement.SetAttribute("RootFolder", String.Format("/sites/{2}/{0}/{1}", sDocLibName, sRootFolderName,sitename))
    

    Tuesday, June 28, 2011 2:02 AM

All replies

  • Surendar,

    If you know the destination document library address/URL, you can use SharePoint web services to do this operation in the event handlers.

    For copying a document, you can use the Copy Web Service:
    http://msdn.microsoft.com/en-us/library/copy.copy.copyintoitems.aspx

    To delete the files from library, you can use the UpdateListItems method under Lists web service. If you use appropriate xmlnode, you can delete the file from the library.
    http://msdn.microsoft.com/en-us/library/lists.lists.updatelistitems.aspx

    <Batch OnError="Continue" ListVersion="1" >
      <Method ID="1" Cmd="Delete">
       <Field Name='ID'>2</Field>
      </Method>  
    </Batch>
    

    Thanks,


    Veera Reddy Kolan
    SharePoint Consultant
    Blog:http://veerareddykolan.blogspot.com/
    Monday, June 20, 2011 1:10 PM
  • Hi,

    I have a small problem when i use the above said lists.asmx. By using the lists.asmx i have no problem in creating a Folder inside a document library. But when i am trying to create a sub folder (folder inside folder) the service does not thow any error nor it creates the sub folder.

     

     Public Function CreateRootFolder(ByVal sFolderName As String, ByVal sDocLibName As String) As Boolean
      Dim oListService As ListsWebService.Lists = New ListsWebService.Lists
      Dim sXMLCommand As String
      Dim oDoc As XmlDocument = New XmlDocument
      sXMLCommand = "<Method ID='1' Cmd='New'><Field Name='FSObjType'>1</Field><Field Name='BaseName'>" + sFolderName + "</Field> " _
          + "<Field Name='ID'>New</Field>" _
          + "</Method>"
      Dim oElement As XmlElement = oDoc.CreateElement("Batch")
      oElement.SetAttribute("OnError", "Continue")
      oElement.InnerXml = sXMLCommand
      oListService.Credentials = System.Net.CredentialCache.DefaultCredentials
      Dim oNode As XmlNode = oListService.UpdateListItems(sDocLibName, oElement)
     End Function
    
     Public Function CreateSubFolder(ByVal sRootFolderName As String, ByVal sSubFolderName As String, ByVal sDocLibName As String) As Boolean
      Try
       Dim oListService As ListsWebService.Lists = New ListsWebService.Lists
       Dim sXMLCommand As String
       Dim oDoc As XmlDocument = New XmlDocument
       sXMLCommand = "<Method ID='1' Cmd='New'><Field Name='FSObjType'>1</Field><Field Name='ContentType'>SubFolder</Field><Field Name='BaseName'>" + sSubFolderName + "</Field> " _
           + "<Field Name='ID'>New</Field>" _
           + "</Method>"
       Dim oElement As XmlElement = oDoc.CreateElement("Batch")
       oElement.SetAttribute("OnError", "Continue")
       oElement.SetAttribute("RootFolder", String.Format("/{0}/{1}/", sDocLibName, sRootFolderName))
       oElement.InnerXml = sXMLCommand
       oListService.Credentials = System.Net.CredentialCache.DefaultCredentials
       Dim oNode As XmlNode = oListService.UpdateListItems(sDocLibName, oElement)
      Catch ex As Exception
       Debug.Print(ex.ToString)
      End Try
     End Function
    
    


    In the above given code chunk CreateRootFolder method works as expected, but CreateSubFolder method is not creating the required subfolder

    Any suggestions are welcome.

    Thanks in Advance


    • Proposed as answer by Rishikesan Tuesday, June 21, 2011 7:07 AM
    • Unproposed as answer by Rishikesan Tuesday, June 21, 2011 7:08 AM
    Monday, June 20, 2011 8:41 PM
  • Change the below line

     oElement.SetAttribute("RootFolder", String.Format("/{0}/{1}/", sDocLibName, sRootFolderName))

    to

     

     oElement.SetAttribute("RootFolder", String.Format("/{0}/{1}", sDocLibName, sRootFolderName))

     

    Tuesday, June 21, 2011 7:08 AM
  • I have tried those changes for all possible combinations.

    The combinations I have already checked for

    URL/DocLib/RootFolder
    URL/DocLib/RootFolder/
    /DocLib/RootFolder/
    /DocLib/RootFolder
    /RootFolder/
    /RootFolder

    but nothing turned out to be working.

    Regards,

    suren

    Tuesday, June 21, 2011 7:19 AM
  • Hi Surendar,

    oElement.SetAttribute("RootFolder", String.Format("/{0}/{1}", sDocLibName, sRootFolderName))

    this is working like a charm... i tested it in my environment within the same farm... can you try it again

    Regards, Rishi

    • Marked as answer by David HM Tuesday, June 28, 2011 1:19 AM
    Tuesday, June 21, 2011 7:36 AM
  • I have figured out why the above code was not working for me.

     

    oElement.SetAttribute("RootFolder", String.Format("/{0}/{1}", sDocLibName, sRootFolderName))
    


     In the above marked part while specifying the RootFolder I have given “/DocLib/RootFolder”, this will work if the site is a main site collection (as is was in your case)
    Ex. https://somedomain.com


    But my URL was https://somedomain.com/sites/sitename, if the URL is of this kind then RootFolder should be “/sites/sitename/doclibname/RootFolder”.

    So if the URL is of type https://somedomain.com/sites/sitename, the RootFolder should be

     

    oElement.SetAttribute("RootFolder", String.Format("/sites/{2}/{0}/{1}", sDocLibName, sRootFolderName,sitename))
    

    Tuesday, June 28, 2011 2:02 AM