1

I am a beginner in Sharepoint and given a task to write timer jobs.

Once triggered, I want to move documents from one Sharepoint server library to another server library programmatically. So far my findings says that this can only be possible using Client Object Model because I am unable to create the second Sharepoint server (spUrl2) instance with SPWebApplication.Lookup method using Server Object Model and this method returns null.

I am running following code from first Sharepoint server (spUrl1):

SPWebApplication primaryWebApplication = SPWebApplication.Lookup(new Uri("http://spUrl1"));

SPWebApplication secondaryWebApplication = SPWebApplication.Lookup(new Uri("http://spUrl2"));

My question:

Is it possible to do the needful using Server Object Model because this is very convenient and I want the same thing. If not then how can I possibly do this file movement as I couldn't find similar reference related to this on web.

Thanks for your support in advance.

1

First, I'm going to assume that when you say Server you mean Farm, as you wouldn't copy/move files to another Server in the same Farm.

There really isn't a clean way to do this with server-side APIs and I'm not sure why you think you need to go that route anyway.

This is super-easy with the Client Object Model. Just add a reference the Microsoft.SharePoint.Client namespace (Microsoft.SharePoint.Client.dll) in your Timer Job project. That assembly is in the ISAPI folder under SharePoint's 14 hive...typically c:\program files\common files\microsoft shared\web server extensions\14.

Here's one good example of this approach already documented: http://blog.blumshapiro.com/blog/2012/08/22/sharepoint-2010-using-the-client-object-model-to-move-files-and-folders-across-site-collections-and-subsites/

You'll need to modify the example above if you're using something other than Forms Authentication, but that's about it.

Hope this is helpful.

How to specify credentials:

using (ClientContext context = new ClientContext("http://site")) {
    context.Credentials = new NetworkCredential("user", "password", "domain");
    // copy files
}
  • Thanks, trying to use the same example but unable to authenticate at the line srcContext.ExecuteQuery();, following error is coming "The remote server returned an error: (401) Unauthorized.". The server is set to use Windows authentication. How to resolve it without changing the authentication mode of server? – Furqan Safdar May 15 '14 at 6:28
  • I added a small code block above on how to use credentials with your ClientContext object. You will need to provide credentials for both the source and destination while removing the AuthenticationMode and FormsAuthenticationLoginInfo options from both. – Rob D'Oria May 15 '14 at 11:38
0

You can use server side object model for the different farm access.

You can use Client object model, Rest APIs or sharepoint web services.

  • Please elaborate how can i use server side object model specifically by giving some example code. – Furqan Safdar May 14 '14 at 14:43

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.