none
Not able to access the sharepoint url that gives the XML content output from the C# console application. RRS feed

  • Question

  • 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:45 AM

All replies

  • Hi Screenivas,

    Thank you for posting here.

    Based on your description, you want to read xml content from the sharepoint url. The following link may be helpful for you to solve the problem.

    https://support.microsoft.com/en-ie/help/307643/how-to-read-xml-data-from-a-url-by-using-visual-c

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, June 25, 2019 2:15 AM
    Moderator
  • Hi Jack,

    The problem here is that im unable to access the xml file in sharepoint

    im able to access the sharepoint siteurl (https://microsoft.sharepoint.com/SharepointSiteUrl)  which contains the lists(After successful autherization). but im unable to access the url which contains the xml file  (https://microsoft.sharepoint.com/SharepointSiteUrl/_api/lists/getbytitle('SharepointListName')/Items(2)/versions)

    (403 error due to authorization)

    Tuesday, June 25, 2019 4:26 AM
  • Hi Sreenivas,

    Thanks for the feedback.

    I could not test the related code because the lacking of sharepoint siteurl. Therefore, I suggest that you could ask it in the SharePoint forum.

    Thanks for your understanding.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, June 25, 2019 8:08 AM
    Moderator