Provider Hosted App – Copy Document across Site Collections

To copy documents across site collections in provider hosted app, we need to perform the following steps:

  1. Provide appropriate permissions to app
  2. Create client context for the source location
  3. Downloading document stream from source
  4. Create client context for the destination
  5. Uploading document to destination (This step is same as mentined in my previous post)

The approach I am going to display will also help if you want to create another site collection context from the existing one.

For the above scenario, please note the following points:

  1. App should have minimum “Write” permission on “Tenant” for the code to work as we are moving document from one site collection to another.
  2. I have used app-only policy. This is used as user might not have write permission on the whole tenant. The only downside is, in “Created By” field, instead of user name, it shows “SharePoint App”. I created a custom column to save actual user name.

Hope this helps. Happy coding!!


5 thoughts on “Provider Hosted App – Copy Document across Site Collections

  1. Thanks for this helpful post!!!.

    Is there any way to implement the same with out using App only policy and the tenant scope. And the current user will be having permission to the Sitecollection. The intend is, The app should be installed by a normal site collection administrator, not by the Tenant Admin.

    • I don’t think that this is possible without “Tenant” permission. You can avoid “App-Only Policy” if users have proper access to both source and destination site collections but app also needs permission to both SC and that is possible with tenant level permission only. The app model follows either “User+App” policy or “App-only” policy so in either case app needs tenant access as this is cross site collection.

      Hope this helps!

  2. Hi Garima,

    I would like to implement the same concept, but the app will be hosted on one site collection say “A”, the app creates a new site collection called “B”. There is another site collection called “C” where we will have documents in it. Can the app copy the documents from “C” to ” B” from “A” ?

    Your reply is highly appreciated!

    • I have not tried it but I guess this is possible. As I mentioned in the post above, just create context of source and destination using URL and you should be good to go. Hope this helps!

  3. Hi Garima, Thanks for the nice article. Can we implement the the same using TokenHelper.GetS2SClientContextWithWindowsIdentity for sharepoint online ?

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s