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)
{
}
}
}
}
}