3

I am new to sharepoint technology. I want to know if it is possible to read a website hosted in sharepoint. I can see that page after I log in. Can I do that in C#? And do I have API to do the log in process to get the login info?

I tried this code to access my sharepoint site via my C# code:

    ClientContext context = new ClientContext("https://mycompany.sharepoint.com/sites/parts/");
    var credentials = new NetworkCredential(mywindowsusername, mywindowspassword);


    context.Credentials = credentials;
    // The SharePoint web at the URL.
    Web web = context.Web;

    // We want to retrieve the web's properties.
    context.Load(web);

    // Execute the query to the server.
    context.ExecuteQuery();

    // Now, the web's properties are available and we could display 
    // web properties, such as title. 
    System.Console.WriteLine("Web Title");
    System.Console.WriteLine(web.Title);

But I keep getting 403 error.

  • What type of info you want? – Aanchal Sep 23 '14 at 5:36
  • Ask questions explicitly so that the people can respond back better. Could you elaborate what exactly you are looking for? – Zakir HC Sep 23 '14 at 5:53
  • Sorry for not being explicitly. I am not certain myself since I am completely new to this field. There is this sharepoint site in my company intranet. I want to programically read access it. – n179911 Sep 23 '14 at 15:57
  • refer my edited code. – Hardik Kothari Sep 25 '14 at 6:35
1

You wrote that your web site is on SharePoint. In the sample code you provided the url "https://mycompany.sharepoint.com/sites/parts/".

I assume your SharePoint site is hosted on Office 365. In order to log into Office 365 and SharePoint Online you need to login with special credential object and method.

The client object model have a specific class to log into Office 365 and this class will generate ICredentials. Those ICredentials than can be passed to the context to log in. This class is called SharePointOnlineCredentials. All you have to do is to pass login name and password.

The ICredentials then can be passed to the context. Your code the will looks like this:

ClientContext context = new ClientContext("https://mycompany.sharepoint.com/sites/parts/");
var credentials = new SharePointOnlineCredentials(yourloginusername, yourloginpassword);


context.Credentials = credentials;
// The SharePoint web at the URL.
Web web = context.Web;

Then you should be able to do whatever you want.

0

actually nothing wrong with the code, but it could be the authorization or authentication issues, as described in some forums.

Try:

context.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;

or try adding domain in the parameter:

var credentials = new NetworkCredential("theusernameonly", "thepassword", "thedomain");

Hope it helps...

Reference:

Stack Overflow: The remote server returned an error: (403) Forbidden error

MSDN Forum: SharePoint .net CSOM getting "The remote server returned an error: (403) Forbidden"

  • What should I try for 'thedomain'? "mycompany"? or "mycompany.sharepoint.com"? – n179911 Sep 25 '14 at 5:19
  • 1 example, that you have an account in your organization as "RADTECH\radityo", typically this is the full username (including the domain). So "RADTECH" is referring to the domain name, and "radityo" is referring to the username. Typically you would write it as new NetworkCredential("radityo", "Password1234", "RADTECH"); – Radityo Ardi Sep 25 '14 at 5:25
  • Or by any chance, you can try System.Net.CredentialCache.DefaultNetworkCredentials; which will pick up the current user's context when you run the application. You can also alter the user's context by running the application as different user (hold shift and right click, will bring you the context menu "run as different user"). – Radityo Ardi Sep 25 '14 at 5:27
  • thanks for your answers. Unfortunately I tried both ways, none of them works. – n179911 Sep 25 '14 at 16:42
  • @n179911, can you try with another site collection, another administrator to see if you can reproduce the issue? You can set up for free a trial SPO tenant - can you try and see if you can authenticate there as a global admin? It may not be the solution, but may give you an idea what you are doing wrong here. – grisha Nov 29 '14 at 21:38
0

Yes there is Microsoft.SharePoint API used, using below c# code you can connect to SharePoint root site:

SPContext.Current.Site.URL

For subsites you can use the below code:

SPContext.Current.Web.URL

For ClientContext use the below code:

ClientContext context = new ClientContext("https://mycompany.sharepoint.com/sites/parts/");
        WindowsImpersonationContext ctx = null;
        if (!WindowsIdentity.GetCurrent().IsSystem)
        {
            ctx = WindowsIdentity.Impersonate(System.IntPtr.Zero);
            // The SharePoint web at the URL.
            Web web = context.Web;

            // We want to retrieve the web's properties.
            context.Load(web);

            // Execute the query to the server.
            context.ExecuteQuery();

            // Now, the web's properties are available and we could display 
            // web properties, such as title. 
            System.Console.WriteLine("Web Title");
            System.Console.WriteLine(web.Title);
        }
  • Thanks. I google this Microsoft.SharePoint API and find this msdn.microsoft.com/en-us/library/office/…. My question is Does my sharepoint server need to config as a REST service before I can use this API? – n179911 Sep 24 '14 at 0:36
  • I hope that REST services are already running on yoor server, it is by default. Try to visit in browser like server url/_api/Lists If you get any url related to feen in IE, the try url first Link1 – ateet Sep 24 '14 at 4:20
  • I can access this site 'https : / /mycompany.sharepoint.com/sites/parts/' in browser using my windows credentials. If I want to do that using c# code, which should be my root site and which is my subsites? – n179911 Sep 25 '14 at 4:27
  • Your root site is 'mycompany.sharepoint.com' and your site collection is 'parts'. Subsite will be underneath of 'parts' site collection like '..parts/gallery'. So gallery will be subsite. And only only thing that you can do is to create a dotnet or console application and use dotnet client object model. even you can search about dotnet client object model. – ateet Sep 25 '14 at 4:34
  • What's the purpose of storing the WindowsImpersonationContext in ctx if ctx is never referenced? Or does the Impersonate() method do some stuff on the system too? – Taylor Lopez Oct 8 '15 at 18:50
0

I'm not sure what exactly you are looking for in C#, a site or web in sharepoint can be accesses via these codes given below:

SPSite site = new SPSite("Site url");
SPWeb web = site.OpenAllWebs();
string userName = web.CurrentUser.LoginName;
  • I want to access a web page in my company intrasite. The URL has '/sharepoint' in it. So I assume it is hosted by sharepoint. In broswer, I need to log in before I can go to that sharepoint web page. So I am not sure how can I do that programmically. – n179911 Sep 23 '14 at 23:02
  • Do you have access to your company SharePoint server, if yes then only these API will work at server side. Other you have to use dotnet client side object model Try these url Link1 Link2 – ateet Sep 24 '14 at 4:06
  • I can load this site 'ht tps: // mycompany.sharepoint.com/sites/parts/" with my windows credentials. I want to do the same with my c# code. Can you please tell me if that is possible – n179911 Sep 25 '14 at 4:29
  • Yes, possible using dotnet client object model or Rest API or webservices of sharepoint if you don't have access to server. – ateet Sep 25 '14 at 4:34
  • Thanks @ateet. I tried the above code with my windows credentails. But I keep getting 403 error. Can you please tell me what I am doing wrong? Thanks. – n179911 Sep 25 '14 at 4:58

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.