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:
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'.
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