У меня есть список A со столбцами и другое имя столбца списка B, такое же, как в списке A, все текст однострочный, и я хочу получить все данные - PullRequest
0 голосов
/ 02 января 2019

У меня есть список A со столбцами Название, Emp ID и Дата начала и окончания, Обозначение, в Списке AI есть некоторые данные, и другое имя столбца Списка B такое же, как у Списка A, все они однострочные, и я хочуполучить все данные из списка А. с помощью Visual Studio.

Ответы [ 2 ]

0 голосов
/ 03 января 2019

Ваш вопрос помечен как 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).

0 голосов
/ 03 января 2019
//Code

using System.Net;
using Microsoft.SharePoint.Client;

using (ClientContext context = new ClientContext("http://yourserver.sharepoint.com/")) {
    context.Credentials = new NetworkCredential("user", "password", "domain");
    List announcementsList = context.Web.Lists.GetByTitle("List A"); 

    // This creates a CamlQuery that has a RowLimit of 100, and also specifies Scope="RecursiveAll" 
    // so that it grabs all list items, regardless of the folder they are in. 
    CamlQuery query = CamlQuery.CreateAllItemsQuery(100); 
    ListItemCollection items = announcementsList.GetItems(query); 

    // Retrieve all items in the ListItemCollection from List.GetItems(Query). 
    context.Load(items); 
    context.ExecuteQuery(); 
    foreach (ListItem listItem in items) 
    { 
        // We have all the list item data. For example, Title. 
        label1.Text = label1.Text + ", " + listItem["Title"]; 
        //label2.Text = label2.Text + ", " + listItem["Emp_x0020_ID"]; 

        //EMP_x0020_ID could be internal name of SharePoint list column "EMP ID"        
        //Likewise check the column internal name for all the columns
        //To find internal name, go to list settings and click each column
        //you will see towards the end of the url something like &Field=Emp_x0020_ID
        //"Emp_x0020_ID" is the internal name of the field
    } 
}

Надеюсь, это поможет

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...