0

I am developing a C# console application. My target SharePoint server is SharePoint Online on Office 365.

I am trying to get the URL of the file present in the SharePoint. I am getting an error:

"The remote server returned an error: (403) Forbidden".

class Program
{
    static void Main(string[] args)
    {
        Web web = null;
        List list = null;
        ClientContext ctx = null;
        ObjectSharingInformation sharingInfo = null;
        string siteUrl = string.Empty;
        string userName = string.Empty;
        string password = string.Empty;
        //Site Url to scan  
        siteUrl = "";
        //Login User name  
        userName = "";
        //Login password  
        password = "";
        //Creating SharePoint Client Context  
        using (ctx = new ClientContext(siteUrl))
        {
            try
            {
                SecureString passWord = new SecureString();
                foreach (char c in password.ToCharArray())
                    passWord.AppendChar(c);


                ctx.Credentials = new SharePointOnlineCredentials(userName, passWord);
                                  
                web = ctx.Web;
                //loading sharepoint web instance  
                ctx.Load(web);
                ctx.ExecuteQueryRetry();
                //get list by using list name  
                list = web.Lists.GetByTitle("Documents");
                // This will get first 100 items from the list including folders.  
                CamlQuery query = CamlQuery.CreateAllItemsQuery(100);
                ListItemCollection items = list.GetItems(query);
                // Retrieve items from List  
                ctx.Load(items, Itms => Itms.Include(Itm => Itm.Id));
                ctx.ExecuteQuery();
                if (items != null && items.Count > 0)
                {
                    foreach (ListItem item in items)
                    {
                        try
                        {
                            sharingInfo = ObjectSharingInformation.GetObjectSharingInformation(ctx, item, false, true, false, true, true, true, true);
                            ctx.Load(sharingInfo);
                            ctx.ExecuteQueryRetry();
                            // Getting Anonymous Edit Link from sharing object  
                            string anonymousEditLink = sharingInfo.AnonymousEditLink;
                            // Getting Anonymous View Link from sharing object  
                            string anonymousViewLink = sharingInfo.AnonymousViewLink;
                            if (sharingInfo != null && sharingInfo.SharedWithUsersCollection != null)
                            {
                                // Looping all shared users information from ObjectSharingInformation  
                                foreach (ObjectSharingInformationUser sharingUser in sharingInfo.SharedWithUsersCollection)
                                {
                                    try
                                    {
                                        if (!string.IsNullOrEmpty(sharingUser.LoginName))
                                        {
                                            string sharingUserLoginName = sharingUser.LoginName;
                                            Console.WriteLine("Shared User Login Name: " + sharingUserLoginName);
                                        }
                                    }
                                    catch (Exception ex) { }
                                }
                            }
                            if (sharingInfo != null && sharingInfo.SharingLinks != null)
                            {
                                // Looping all sharing links from ObjectSharingInformation  
                                foreach (SharingLinkInfo sharingLinkInfo in sharingInfo.SharingLinks)
                                {
                                    try
                                    {
                                        if (!string.IsNullOrEmpty(sharingLinkInfo.Url))
                                        {
                                            string sharingLink = sharingLinkInfo.Url;
                                            Console.WriteLine("Sharing Link Url: " + sharingLink);
                                        }
                                    }
                                    catch (Exception ex) { }
                                }
                            }
                        }
                        catch (Exception ex) { }
                    }
                }
                Console.ReadLine();
            }
            catch (Exception ex)
            {
            }
        }
    }   
}
}

1 Answer 1

0

Please make sure you have enough permissions to access this site. You could try to access this site through browser, check if it is work.

Although you may use SharePoint admin or Global admin account, you still need to add yourself as the site admins to access this site: https://docs.microsoft.com/en-us/sharepoint/manage-site-collection-administrators

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.