2

I am having troubles to get it running and wanted to ask you if you know if it is possible to use CSOM in SP 2013 Farm Solutions?

I constantly get an 401 unauthorized exception.

Here is how I create the clientcontext:

this.webUrl = SPContext.Current.Web.Url;
this.siteUrl = SPContext.Current.Site.Url;

clientContext = new SP.ClientContext(this.webUrl);
clientContext.Credentials = CredentialCache.DefaultNetworkCredentials;

As I want to share the code between an SharePoint Autohosted app and my farm solution I have to use the CSOM.

1
  • Are you building a separate web application, similar to an autohosted one, that will connect to your SharePoint and you want users to be able to authenticate to SharePoint through this web application?
    – eirikb
    Commented Apr 22, 2014 at 11:31

1 Answer 1

1

This won't work because of internal security checks of SharePoint, and deactivating them isn't recommended.

Therefore you abstract your main code and create a semi intelligent data binding layer in your class to load the data. Dynamically loading in the required namespace for reading in the data, by using reflection.

System.Reflection.Assembly asmbly;
asmbly = System.Reflection.Assembly.Load("Microsoft.SharePoint")

object clss;
clss = asb.CreateInstance("Microsoft.SharePoint.Foo")

Yes it would be a little more code, but if you abstract it properly and load the data into a List for example, then this code will be minimal.

Reference - Dynamic Namespace Imports and Switching:

http://www.codeproject.com/Articles/32828/Using-Reflection-to-load-unreferenced-assemblies-a

http://www.debenu.com/kb/switching-between-the-32-bit-and-64-bit-dll-versions-of-debenu-quick-pdf-library/

5
  • If what you stated works then it's awesome !! Commented Apr 22, 2014 at 11:16
  • I use it on a project to switch between SPO and Farm, same concept.
    – Hugh Wood
    Commented Apr 22, 2014 at 11:25
  • Ok thank you for your answer. How are developers supposed to handle this situation? Is it best practice to load the assembly with reflection?
    – AdelPoint
    Commented Apr 23, 2014 at 7:53
  • Best practice, maybe not, but if you NEED a single solution which I have needed before then it is a practice you could follow. However the best would be a single data processing library and 2 separate solutions importing that library which have the layer to get the data from the source.
    – Hugh Wood
    Commented Apr 23, 2014 at 8:26
  • Yes, that is exactly what I have. Two solutions: 1 on premise and 1 app solution and they both share the data layer. In this data layer I am trying to use the CSOM so both solutions could use it... So there is no way around your solution i guess... Thank you I will implement it the way you suggested
    – AdelPoint
    Commented Apr 23, 2014 at 9:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.