locked
Not able to access sharepoint url(generates XML output) directly from c# console application RRS feed

  • General discussion

  • When im trying to access the sharepointUrl its showing a 403 forbidden error in C#. I can able to access the sharepoint url using client context as below.

                    var strUserName = ConfigurationManager.AppSettings["UserName"].ToString();
                    var strPassword = ConfigurationManager.AppSettings["Password"].ToString(); //names[1];
                    string url = @"https://microsoft.sharepoint.com/SharepointSiteUrl";
                    var context = new ClientContext(url);
                    SecureString password = new SecureString();
                    foreach (var i in strPassword) password.AppendChar(i);
                    context.Credentials = new SharePointOnlineCredentials(strUserName, password);
                    Web web = context.Web;
                    context.Load(web);
                    context.ExecuteQuery();

    When i replaces the url to  https://microsoft.sharepoint.com/SharepointSiteUrl/_api/lists/getbytitle('SharepointListName')/Items(2)/versions this is givivng 403 error.

    when i'm accessing both the urls in browser https://microsoft.sharepoint.com/SharepointSiteUrl, this is navigating to microsoft credntials page.

    whereas when i clear the cookies and access the below url https://microsoft.sharepoint.com/SharepointSiteUrl/_api/lists/getbytitle('SharepointListName')/Items(2)/versions is throwing error as Access denied. You do not have permission to perform this action or access this resource.

    This is giving XML output only after accesing to  https://microsoft.sharepoint.com/SharepointSiteUrl and after giving the credentials to this then only im accessing the https://microsoft.sharepoint.com/SharepointSiteUrl/_api/lists/getbytitle('SharepointListName')/Items(2)/versions

    so how can i directly access the url to read the xml File.

    I have also  tried with httpweb

     HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(url);

                System.Net.NetworkCredential credentialCache = new System.Net.NetworkCredential(strUserName, strPassword, strDomain);
                webRequest.Credentials = credentialCache;
    webRequest.Method = "GET";
    webRequest.ContentType = "text/xml";
    HttpWebResponse res = webRequest.GetResponse() as HttpWebResponse; //403 forbidden error

    ----------------------------------------------------------------------------------------------------------------

    I have also tried with Httpclienthandler

     var httpClientHandler = new HttpClientHandler()
                {
                    Credentials = new NetworkCredential(strUserName, strPassword, strDomain),
                };

                var httpClient = new HttpClient(httpClientHandler);
                httpClient.DefaultRequestHeaders.Accept.Clear();
                httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/XML"));

                var result = await httpClient.GetStringAsync(url);// 403 
    Monday, June 24, 2019 10:49 AM