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.

  • 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 Sep 23 '13 at 6:18
1

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

| improve this answer | |
  • objContext = new ClientContext.current; this line is not accept in windows application. – Sakthi Sep 23 '13 at 6:38
  • ClientContext objContext = new ClientContext().Current – Robert Lindgren Sep 23 '13 at 7:18
  • this line also not accept – Sakthi Sep 23 '13 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. – Rob Windsor Sep 23 '13 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.

| improve this answer | |

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.