0

i am using C# windows application. i want to access the SharePoint site to client Om. Normally we are using, ClientContext objContext = new ClientContext("//Sharepointsite");

but my question is How to get the current SharePoint client context? not using SharePoint site directly.

1
  • 2
    How will you get the site reference if you don't provide URL. You can do it using javascript but not using C#.
    – Pushpendra
    Commented Sep 23, 2013 at 6:18

2 Answers 2

1

You can use objContext = new ClientContext.current; property to get your current required object.

4
  • objContext = new ClientContext.current; this line is not accept in windows application.
    – Sakthi
    Commented Sep 23, 2013 at 6:38
  • ClientContext objContext = new ClientContext().Current Commented Sep 23, 2013 at 7:18
  • this line also not accept
    – Sakthi
    Commented Sep 23, 2013 at 7:46
  • 3
    There is no context in a Windows Application. Your only option is to pass the site or web URL to the ClientContext constructor. Commented Sep 23, 2013 at 14:07
1

In order to get a context in a Windows App using CSOM, you still need to create a context. In C# with CSOM, you'd use:

using Microsoft.SharePoint.Client;

ClientContext cc = new ClientContext(Settings.Default.SiteName);
cc.AuthenticationMode = ClientAuthenticationMode.Default;
string username = Settings.Default.UserName;
string password = Settings.Default.Password;
string domain = Settings.Default.Domain;
NetworkCredential credentials = new NetworkCredential(username, password, domain);
cc.Credentials = credentials;

//Get a reference to the Web
Web web = cc.Web;
//pass a parameter to get a list...
List sourceList = web.Lists.GetByTitle(list);
cc.Load<List>(sourceList);

//...or hardcode the list name...
List products = web.Lists.GetByTitle("Items");
cc.Load<List>(products);

If don't get a context this way, you don't have a Context, Site or Web to reference with the CSOM nor the actual data.

You can use REST to access the data, but you still end up having to reference the site.

One more note: as a Windows App, not using CSOM, if you attempt to use the client Object model you will get a NULL for SPContext if you try to read it directly...because once again, you don't HAVE a Context, so you get a null.

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.