1

I'm new to SharePoint programming and have the following problem:

I added a List to my Visual Studio SP project (SampleAddInList). Now I want to get the list in my program to fill in an example item.

Here is the Screenshot of my Visual Studio project with the list: enter image description here

Here my code where I try to get the list to add an item:

private void AddUserToList(string accessToken)
    {
        if (IsPostBack)
        {
            sharepointUrl = new Uri(Request.QueryString["SPHostUrl"]);
        }


        ClientContext clientContext =
                TokenHelper.GetClientContextWithAccessToken(
                    sharepointUrl.ToString(), accessToken);


        // Load the properties for the web object.
        Web web = clientContext.Web;
        clientContext.Load(web);
        clientContext.ExecuteQuery();

        // Get the current user.
        clientContext.Load(web.CurrentUser);
        clientContext.ExecuteQuery();
        currentUser = clientContext.Web.CurrentUser.Email;

        // Load the list "SampleAddInUserList"
        List userList = web.Lists.GetByTitle("SampleAddInList");
        clientContext.Load<List>(userList);
        clientContext.ExecuteQuery();
        ListItemCreationInformation info = new ListItemCreationInformation();

        Microsoft.SharePoint.Client.ListItem newItem = userList.AddItem(info);
        newItem.ParseAndSetFieldValue("Title", "Test");
        newItem.ParseAndSetFieldValue("Email", currentUser);
        newItem.Update();
        clientContext.ExecuteQuery();
    }

When I run the project, I get the following error:

List 'SampleAddInList' does not exist at site with URL 'https://xxx.sharepoint.com/sites/test'.

Here the error screenshot: enter image description here

It seems that the app tries to get the list from the SP test Site, but the list is missing there (the project should create the list by itself if not exisiting, shouldn't it??).

When I try to use a list (another list name!) which i created on SP Web UI via "Add an app", the access works fine.

Can someone give a clue???

Has this something to do with the context URL I use??? see also my other question: Sharepoint Online: Difference between SPAppWebUrl & SPHostUrl

Community
  • 1
  • 1
D. Müller
  • 3,336
  • 4
  • 36
  • 84

1 Answers1

1

It seems that the app tries to get the list from the SP test Site, but the list is missing there (the project should create the list by itself if not exisiting, shouldn't it??).

That's right, the error occurs since ListCollection.GetByTitle method throws exception if list does not exist.

You could consider the following extension method to get or create a List if it does not exist:

using System.Linq;
using Microsoft.SharePoint.Client;

namespace SharePoint.Client.Extensions
{
    public static class ListCollectionExtensions
    {
        public static List EnsureList(this ListCollection lists, string listTitle, ListTemplateType listTemplateType)
        {
            var ctx = lists.Context;
            var result = ctx.LoadQuery(lists.Where(l => l.Title == listTitle));
            ctx.ExecuteQuery();
            if (!result.Any())
            {
                var lci = new ListCreationInformation();
                lci.Title = listTitle;
                lci.TemplateType = (int)listTemplateType;
                var list = lists.Add(lci);
                ctx.Load(list);
                ctx.ExecuteQuery();
                return list;
            }
            return result.FirstOrDefault();
        }
    }
}

Usage

var customList = ctx.Web.Lists.EnsureList("Notes", ListTemplateType.GenericList);
var documentsLibrary = ctx.Web.Lists.EnsureList("Documents", ListTemplateType.DocumentLibrary);
var tasksList = ctx.Web.Lists.EnsureList("Tasks",ListTemplateType.TasksWithTimelineAndHierarchy);
Vadim Gremyachev
  • 57,952
  • 20
  • 129
  • 193
  • But I have the following problem: I already created a list in the SP part of my app, where the columns are already defined - Is there a way to change your code, so that it uses this list? – D. Müller Jan 14 '16 at 13:20
  • in fact the specified method does not create a list if list already exists, it returns the existing one.. – Vadim Gremyachev Jan 14 '16 at 13:32
  • Yes, I just tried this. But my problem is that I added a list via SP project part (add -> List) but can't use it. I get the "List does not exist" exception as described above. I want to use the list I've already added to the project (see first screenshot) – D. Müller Jan 14 '16 at 13:35