I have a custom sharepoint list called VSList, from which I can retrieve all items (4 columns altogether) using the following code:
string siteUrl = url2;
ClientContext clientContext = new ClientContext(siteUrl);
SP.List oList = clientContext.Web.Lists.GetByTitle("VSList");
CamlQuery camlQuery = new CamlQuery();
ListItemCollection collListItem = oList.GetItems(camlQuery);
clientContext.Load(collListItem, items => items.Include(item => item["Title"], item => item["qf2a"], item => item["_x0077_830"], item => item["u6zl"]));
clientContext.ExecuteQuery();
Is there any way to reverse it somehow, so I can upload data, and not download it?
Thanks in advance.!
This is fairly simple to do with the following code (edited my code to fit your list).
// Open client context to the site
var clientContext = new ClientContext(siteUrl);
// Use client context to open the list
var oList = clientContext.Web.Lists.GetByTitle("VSList");
var listCreationinformation = new ListItemCreationInformation();
var oListItem = oList.AddItem(listCreationinformation);
// Push information to individual tables in the selected list
oListItem["Title"] = value1;
oListItem["qf2a"] = value2;
oListItem["_x0077_830"] = value3;
oListItem["u6zl"] = value4;
oListItem.Update();
clientContext.ExecuteQuery();
To get a better overview on how to do this see - How to: Create, Update, and Delete List Items
Related
I am trying to use an AppOnlyAuthenticated context in order to loop over all the documents in a specific list in Sharepoint. The reason to use the AppContext is that this needs to be automated (part of a script) and using an actual user accounts seems like the wrong choice.
Repro:
// using OfficeDevPnp.Core;
// using Microsoft.SharePoint.Client;
var webUrl = "https://company.sharepoint.com/sites/test";
var appId = "xXXXX-xxxx-.....";
var appSecret = "longSecret";
var listTitle = "ListTitle";
var am = new AuthenticationManager();
using (var context = am.GetAppOnlyAuthenticatedContext(webUrl, appId, appSecret))
{
var lists = context.Web.Lists;
var list = lists.GetByTitle(listTitle);
var listItems = list.GetItems(CamlQuery.CreateAllItemsQuery(rowLimit = 5));
context.Load(lists);
context.Load(list);
context.Load(listItems, items => item.Include(i => i.Id));
context.ExecuteQuery();
var list_ItemCount = list.ItemCount;
var listItems_Count = listItems.Count;
}
}
The list_ItemCount has the correct value of items in that list of files but listItems_Count is zero. Obviously, when trying to loop over listItems, there is nothing to enumerate.
Could it be a permission thing? Or is something else causing the issue?
I am trying to get a list of items from a specific view. Below is the code
Microsoft.SharePoint.Client.List _lists = context.Web.Lists.GetByTitle("Invoice Register");
context.Load(_lists);
context.ExecuteQuery();
int listCount = _lists.ItemCount; // i get 49000+ count here
View _listsView = _lists.Views.GetByTitle("IT Testing");
context.Load(_listsView);
context.ExecuteQuery();
CamlQuery _query = new CamlQuery();
_query.ViewXml = _listsView.ViewQuery;
Microsoft.SharePoint.Client.ListItemCollection items = _lists.GetItems(_query);
context.Load(items);
context.ExecuteQuery();
int _viewCount = items.Count; // I get nothing here.
The error I get is The attempted operation is prohibited because it exceeds the list view threshold enforced by the administrator
I have created indexes already
I have set the limit for "IT Testing" to 5000 as seen here.
If someone can guide, it would be helpful. I have gone through all the links I could.
Regards
#Ather Siddiqui,
there is no direct way to get items under a view as the view only has query schema and does not have any items. The caml query may get the same items that are under the view, but it will trigger the list view threshold.
#user2250152 has provided a good method, it's possible to get over 5000 items through Pagination. If you want to use the view query, you could change the query as below:
List tList = context.Web.Lists.GetByTitle("My test list");
CamlQuery camlQuery = new CamlQuery
{
ViewXml = #"<View><Query><Where><Gt><FieldRef Name='ID' /><Value Type='Counter'>20</Value></Gt></Where></Query><OrderBy><FieldRef Name ='FileLeafRef' /></OrderBy><RowLimit>4990</RowLimit></View>" // your view query
};
var itemColl = new List<ListItem>();
do
{
ListItemCollection listItemCollection = tList.GetItems(camlQuery);
context.Load(listItemCollection);
context.ExecuteQuery();
//
itemColl.AddRange(listItemCollection);
camlQuery.ListItemCollectionPosition = listItemCollection.ListItemCollectionPosition;
} while (itemColl.Count < 999); //view row limit
Console.WriteLine(itemColl);
I'm using this code to load a lot of items.
var itemsPerPage = 100;
var query = CamlQuery.CreateAllItemsQuery(itemsPerPage);
var loadMoreItems = false;
do
{
var items = list.GetItems(query);
ctx.Load(items);
ctx.ExecuteQuery();
loadMoreItems = items.Count == itemsPerPage;
query.ListItemCollectionPosition = items.ListItemCollectionPosition;
}
while (loadMoreItems);
It's based on this
CalmQuery.CreateAllItemsQuery
Can you help me getting the items of a SharePoint library (it is a library with document sets) using CSOM.
The library has a lot more than 5000 items and I know SharePoint has a limit of 5000, so I am trying to get them in batches of 500. Is it possible?
clientContext.Credentials = new SharePointOnlineCredentials(username, securePassword);
List oList = clientContext.Web.Lists.GetByTitle(libraryName);
clientContext.Load(oList);
clientContext.ExecuteQuery();
CamlQuery query = new CamlQuery();
query.ViewXml = "<View><RowLimit>500</RowLimit></View>";
ListItemCollection allDocumentSet = oList.GetItems(query);
clientContext.Load(allDocumentSet);
clientContext.ExecuteQuery();
I do not know why is not working. I get the following error.
Microsoft.SharePoint.Client.ServerException: 'The attempted operation is prohibited because it exceeds the list view threshold enforced by the administrator.'
Why do I get the error if I am getting only 500 items per batch?
Also, one additional question. When should I use "ExecuteQuery" and Load function?
Thanks!
You need run ExecuteQuery to retrieve/update the data from server.
Here is my sample test code to retrieve items more than 5000.
List lmsList = clientContext.Web.Lists.GetByTitle("LargeList");
ListItemCollectionPosition itemPosition = null;
while (true)
{
CamlQuery camlQuery = new CamlQuery();
camlQuery.ListItemCollectionPosition = itemPosition;
camlQuery.ViewXml = #"<View><RowLimit>500</RowLimit></View>";
ListItemCollection listItems = lmsList.GetItems(camlQuery);
clientContext.Load(listItems);
clientContext.ExecuteQuery();
itemPosition = listItems.ListItemCollectionPosition;
Console.WriteLine(itemPosition);
//foreach (ListItem listItem in listItems)
// Console.WriteLine("Item Title: {0}", listItem["Title"]);
if (itemPosition == null)
break;
Console.WriteLine(itemPosition.PagingInfo);
}
Problem: In the foreach below, I have 2 parts.
Part 1 opens an excel File and modifies it
Part 2 updates 3 of the ListItem fields
Both parts work individually but with them both enabled, Part 2 fails do to Microsoft.SharePoint.Client.ServerException: 'Version conflict.' understandably
Question: How can I refresh/reload my ListItem between Part 1 and Part 2?
I tried ListItem.RefreshLoad();
And I looked all over for something along the lines of ListItem.GetFromURL(); but can't seem to find what I'm looking for
ClientContext cc = new ClientContext(site)
SecureString pass = new SecureString();
Web web = cc.Web;
foreach (char c in "password".ToCharArray())
{
pass.AppendChar(c);
}
cc.Credentials = new SharePointOnlineCredentials("User#Domain.com", pass);
Microsoft.SharePoint.Client.List list = web.Lists.GetByTitle("MyLibrary");
CamlQuery qry = new CamlQuery
{
ViewXml = "<View><Query><Where><Contains><FieldRef Name='Active'/><Value Type='Boolean'>1</Value></Contains></Where></Query></View>"
};
Microsoft.SharePoint.Client.ListItemCollection listItems = list.GetItems(qry);
cc.Load(listItems);
cc.ExecuteQuery();
foreach (Microsoft.SharePoint.Client.ListItem listItem in listItems)
{
//PART 1
//Open the file
//Modify the File
//Save the file
//PART 2
listItem["Updated"] = DateTime.Now;
listItem["Update_x0020_Duration"] = totalTime;
listItem["System_x0020_Comment"] = "TestComment";
listItem.Update();
cc.ExecuteQuery();
}
From my experience Microsoft.SharePoint.Client namespace can be a bit tricky. I have struggled with the same issue in the past.
This is the general idea based on your code. please read comments:
ClientContext cc = new ClientContext(site);
// try to use the List object, using the name of the list as the parameter
List oList = cc.Web.Lists.GetByTitle(listName);
// pass the credantials!
cc.Credentials = new System.Net.NetworkCredential(< username >, < password >, < domain >);
// another option - default NetworkCredentials credantials
// context.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;
// pass the uniq id of the record
ListItem listItem = oList.GetItemById(id);
// now you can to this
// ...............
listItem["System_x0020_Comment"] = "TestComment";
listItem.Update();
cc.ExecuteQuery();
I have a list called "Books" with columns 'Name','AuthorName','ISBN' with type as text. Now I have another list called "BillTokenStore" and i want to add lookup column 'AuthorName' in "BillTokenStore". Below is what i have done.
using (ClientContext context = new ClientContext(webFullUrl: siteUrl))
{
context.Credentials = new SharePointOnlineCredentials(userName, GetPassWord());
Web web = context.Web;
ListCollection listCollection = web.Lists;
List list = listCollection.GetByTitle("BillTokenStore");
string schemaLookupField = #"<Field Type='Lookup' Name='InStock' StaticName='InStock' DisplayName='InStock' List = 'Books' ShowField = 'Title' /> ";
Field lookupField = list.Fields.AddFieldAsXml(schemaLookupField, true, AddFieldOptions.DefaultValue);
context.ExecuteQuery();
}
When i run this code, i am getting the error "value does not fall within the expected range sharepoint 2013". What is wrong here? Thanks in Advance.
Note: I am able to achieve the same thing thorough UI. I am also able to add other type of fields like choice,boolean and all through code.
You need to explicitly load the list and the fields of that list.
Also, we need to pass the GUID of the lookup column list.
Please try the below modified code:
using (ClientContext context = new ClientContext(webFullUrl: siteUrl))
{
context.Credentials = new SharePointOnlineCredentials(userName, GetPassWord());
Web web = context.Web;
List booksList = context.Web.Lists.GetByTitle("Books");
List list = context.Web.Lists.GetByTitle("BillTokenStore");
context.Load(list, l => l.Fields);
context.Load(booksList, b => b.Id);
context.ExecuteQuery();
string schemaLookupField = #"<Field Type='Lookup' Name='InStock' StaticName='InStock' DisplayName='InStock' List='"+ booksList.Id +"' ShowField = 'Title' />";
Field lookupField = list.Fields.AddFieldAsXml(schemaLookupField, true, AddFieldOptions.DefaultValue);
lookupField.Update();
context.Load(lookupField);
context.ExecuteQuery();
}