Ваш вопрос помечен как sharepoint-online, поэтому этот код будет работать для вас. Вместо NetworkCredential
(как указано в ответе @Dhurba) вы должны использовать SharePointOnlineCredentials
.
string userName = "";
string password = "";
string siteUrl = "https://tenant.sharepoint.com";
using (ClientContext cc = new ClientContext(siteUrl))
using (SecureString securedPassword = new SecureString())
{
password.ToList().ForEach(c => securedPassword.AppendChar(c));
cc.Credentials = new SharePointOnlineCredentials(userName, securedPassword);
// Load only server relative url
cc.Load(cc.Web, p => p.ServerRelativeUrl);
cc.ExecuteQuery();
// Get list by url - better than by title, because title can be different in other languages
List list = cc.Web.GetList($"{cc.Web.ServerRelativeUrl.TrimEnd('/')}/Lists/SomeListUrl");
// Load all items even if the list contains more than 5000 items,
// which is hard limit in SharePoint Online, using paggination
CamlQuery query = CamlQuery.CreateAllItemsQuery(200);
do
{
ListItemCollection items = list.GetItems(query);
cc.Load(items);
cc.ExecuteQuery();
foreach (ListItem item in items)
{
Console.WriteLine($"ID: {item.Id} | {item["InternalNameOfColumn"]}");
}
// Set position info to query
query.ListItemCollectionPosition = items.ListItemCollectionPosition;
// When there are no other items the position info will be null
} while (query.ListItemCollectionPosition != null);
}
Примечание:
Если вы используете Visual Studio старше, чем 2017, интерполяция $"{someVariable}"
не будет работать, и вам придется заменить ее, например, на. string.Format("{0}", someVariable)
.