1
votes

I am not familiar with Sharepoint and need to get data from it to use in an application using CSOM.

I'm able to connect and retrieve data for lists on the site. The problem I have is that a lot of the data I need is wrapped up in what I've been told is a "SharePoint News Feed".

Below is what I have so far but it doesn't work. listItemCollection seems to get populated (17 items) but I can't see any properties that enable me to get at the data.

How do I get data from pages in the SitePages ?

public class VacancyService : IVacancyService
{
    private readonly ISiteCredentialService _siteCredentialService;

    private const string SharePointSiteUrl = "https://[my].sharepoint.com/vacancies/";

    public VacancyService(ISiteCredentialService siteCredentialService)
    {
        _siteCredentialService = siteCredentialService;
    }

    public List<VacancyDto> GetData()
    {
        List<VacancyDto> result = new List<VacancyDto>();

        using (var context = new ClientContext(SharePointSiteUrl))
        {
            context.Credentials = _siteCredentialService.GetSharepointOnlineCredentials();

            // Assume the web has a list named "Vacancys". 
            var listTitle = "Site Pages";

            List list = context.Web.Lists.GetByTitle(listTitle);
            context.Load(list);

            CamlQuery cQuery = new CamlQuery();
            ListItemCollection listItemCollection = list.GetItems(cQuery);

            context.Load(listItemCollection, items => items.Include(item => item));

            context.ExecuteQuery();

            foreach (ListItem listItem in listItemCollection)
            {
                string itemTitle = listItem.GetFieldValueString("Title");

                VacancyDto dto = new VacancyDto()
                {
                    Title = itemTitle
                };

                result.Add(dto);
            }

        }

        return result;
    }
}
2

2 Answers

3
votes

If you don't specify the columns, you won't get them back from the call. You should specify the columns that you want to receive from the call, this can be achieved in two ways:

Method 1 (View XML):

camlQuery.ViewXml = "<View><Query><Where><Geq><FieldRef Name='ID'/>" +
            "<Value Type='Number'>10</Value></Geq></Where></Query><RowLimit>100</RowLimit></View>";

Method 2 (include method):

context.Load(collListItem, items => items.Include(item => item.Id,
                item => item.DisplayName,
                item => item.HasUniqueRoleAssignments));

Source

1
votes

I was having issues with the strongly typed "fields", and I found the following solution:

    var list = context.Web.Lists.GetByTitle("Site Pages");
    var items = list.GetItems(CamlQuery.CreateAllItemsQuery());
    context.Load(items, icol => icol.Include(i => i["DisplayName"], i => i["Id"], 
                                   i => i["ID"], i => i.ContentType, i => i["Editor"]));
    context.ExecuteQuery();

This way you can specify the field names in double quotes. It also helps to look in the QuickWatch (items => Results View => [0]) to see which fields VisualStudio is trying to populate. Adding any fields that show a red and the value says something like "The property or field 'FileSystemObjectType' has not been initialized..." will get those values for you (most likely).