I want to update "created by" column in sharepoint. For this, i have to do using CSOM only as i do not have sharepoint server dll. I wrote code in this...
ClientContext cc = new ClientContext ("http://sharepoint...");
List list = cc.web.Lists.GetByTitle("sharepointlistname");
cc.Load(list);
ListItem item = list.GetItemById(13);//In this case, i want to update only id=13. I want to know code for all records also.
cc.Load(item);
cc.ExecuteQuery();
item("Created by") = "Dinesh";
item.Update();
When I run above code, am getting this error...
sharepoint list Invalid data has been used to update the list item. The field you are trying to update may be read only
The Created By field internal name is Author and it's a Person field, please update like this:
ClientContext ctx = new ClientContext("http://sp/sites/dev");
List list = ctx.Web.Lists.GetByTitle("MyList");
Microsoft.SharePoint.Client.CamlQuery camlQuery = new CamlQuery();
//CamlQuery to filter items which created in Today
camlQuery.ViewXml =
#"<View>
<Query>
<Where><Eq><FieldRef Name='Created' /><Value Type='DateTime'><Today /></Value></Eq></Where>
</Query>
</View>";
ListItemCollection items = list.GetItems(camlQuery);
ctx.Load(items);
ctx.ExecuteQuery();
User theUser = ctx.Web.EnsureUser("Contoso\\Jerry");
ctx.Load(theUser);
ctx.ExecuteQuery();
foreach (var item in items)
{
item["Editor"] = theUser;
item["Author"] = theUser;
item.Update();
}
ctx.ExecuteQuery();
Related
In my project, i have to show all the name of items of the Library path that user give me. If no folder the library contains => no problem.
However, there are many folders, sub-folders, sub-sub-folders, sub^n-folders. I cannot loop all the folders, which get a endless loop. I don't need the code that i just need the concept of how to loop all folders.
Therefore, i need help from all of you experienced programmer.
The result should be:
item name folder name
---------------------------------------------
item1
item2
item1 subF1
item1 subF2
item1 subF2sub1
.
.
My idea:
// path of library and folder is URL. For example, http://example.com/libraryName/subF2/subF2sub1/item1
//if i get all folders'path, i can then get the file name by those paths.
// i won't paste my code here because there are >100 lines.
void checkFolderExist(libraryPathByUser)
{
if("folderInLibrary" != nil)
{
foreach (var folder in library)
{
string folderPath = getFolderPath(folder);
strList.Add(folderPath);
// so, how about sub-folder in the folder?
}
}
}
You can create a SPQuery object and set its Scope attribute to RecursiveAll, for example:
SPQuery query = new SPQuery();
SPFolder folder = get the folder object by folder path
query.Folder = folder;
query.ViewXml = "<View Scope=\"RecursiveAll\"><Query>your query goes here</Query></View>";
SPListItemCollection items = yourLibrary.GetItems(query);
Dictionary<string, List<SPListItem>> folderItems = new Dictionary<string, SPListItem[]>();
foreach (SPListItem item in items)
{
// If items are files
SPFile file = item.Web.GetFile(item.Url);
string folderName = file.ParentFolder.Name;
if (!folderItems.ContainsKey(folderName))
{
folderItems[folderName] = new List<SPListItem>();
}
folderItems[folderName].Add(item);
}
Following code will recursively search all the artifacts and store in listItem object. It is using csom though.
ClientContext clientContext = new ClientContext("http://Servername/");
List sharedDocumentsList = clientContext.Web.Lists.GetByTitle("Shared Documents");
CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml =
#"<View Scope='Recursive' />";
ClientOM.ListItemCollection listItems =
sharedDocumentsList.GetItems(camlQuery);
clientContext.Load(listItems);
clientContext.ExecuteQuery();
foreach (var item in listItems)
{
}
Not sure if you are looking for Clientside code or not.
I have a
Microsoft.SharePoint.Client.ServerException: Value does not fall
within the expected range
I cant change the List View Lookup Threshold, because it is not in a Server environment and I dont have access to it.
I would need to get the text that is displayed in the list instead of getting the hyperlink (hence the error).
This is the code I`m using:
clientContext.Load(collListItem, items => items.Include(
item => item["IT_x0020_Services"]));
clientContext.ExecuteQuery();
ArrayList itserv = new ArrayList();
foreach (ListItem oListItem in collListItem)
{
itserv.Add(oListItem["IT_x0020_Services"].ToString());
}
What am I missing?
Are you missing the CamlQuery?
I tried your code just changing "IT_x0020_Services" to "Title" on a list I have here and it worked properly for me.
ClientContext clientContext = new ClientContext("http://127.0.0.1");
var oList = clientContext.Web.Lists.GetByTitle("TestList");
CamlQuery camlQuery = new CamlQuery();
ListItemCollection collListItem = oList.GetItems(camlQuery);
clientContext.Load(
collListItem,
items => items.Include(
item => item["Title"]));
clientContext.ExecuteQuery();
ArrayList itserv = new ArrayList();
foreach (ListItem oListItem in collListItem)
{
itserv.Add(oListItem["Title"].ToString());
}
We have one SharePoint Server and another server which is having our web service (SharePoint not installed on the machine). We are trying to access SharePoint server list items using our web service. We are getting count of list items but when we go for CAML query, it returns 0 items.
How can we proceed for getting the list item?
here is our code for your reference.
List Device_List = context.Web.Lists.GetByTitle("MasterList");
context.Load(Device_List);
context.ExecuteQuery();
int position = Device_List.ItemCount;
CamlQuery query = new CamlQuery();
query.ViewXml = #"<View><Query><Where><Eq><FieldRef Name='key' /><Value Type='Text'>ConTransDB</Value></Eq></Where></Query></View>";
ListItemCollection itemCollection = Device_List.GetItems(query);
context.Load(itemCollection);
context.ExecuteQuery();
if (itemCollection.Count > 0 && itemCollection != null)
{
string value = itemCollection[0]["value"].ToString();
}
The ViewXml is not complete, it requires you to define the complete view, not just the query. So it would look like:
query.ViewXml = "<View><Query><Where>...</Where></Query></View>";
You can even add more info than just the query, for example:
<View>
<Query>
<Where>...</Where>
<OrderBy>...</OrderBy>
</Query>
<RowLimit>...</RowLimit>
<ViewFields>...</ViewFields>
</View>
I got a list in sharepoint and i want to be able to print the fields out from the list. The list contains First Name, Last Name, Title and some other things.
My problem is i dont know how to write the first name and last name out and the other fields in the list? I hope someone can help :) Thanks :)
This is my c# code.
using (ClientContext clientContext = new ClientContext("MySite"))
{
SecureString passWord = new SecureString();
foreach (char c in "MyPassword".ToCharArray())
passWord.AppendChar(c);
clientContext.Credentials = new SharePointOnlineCredentials("MyAccount", passWord);
SP.List oList = clientContext.Web.Lists.GetByTitle("MyList");
CamlQuery camlQuery = new CamlQuery();
ListItemCollection collListItem = oList.GetItems(camlQuery);
clientContext.Load(collListItem);
clientContext.ExecuteQuery();
foreach (ListItem oListItem in collListItem)
{
//HERE!!! I want to write first name and last name out! But how?
Console.WriteLine();
}
Console.ReadLine();
}
Take a look at this MSDN article, as it may be of some help. http://msdn.microsoft.com/en-us/library/office/ff521580(v=office.14).aspx
Have you tried putting:
Console.WriteLine("{0} {1}",oListItem.columnFirstName.toString(), oListItem.columnLastName.toString());
Note: SharePoint writes the first name a column is given on the backend. When you update that name it updates the friendly version (i.e. what you see in the UI) but the original name is still what you'd access it with.
I'm working in a webpart. I filter (with caml) a SharePoint list and put my results in a List<SPListItem>.
Now, i need to populate another SharePoint List (I created that list in the same code) and i can't find the way to do that.
List<SPListItem> results = new List<SPListItem>() //results have the result of my query
.
.
.
SPList listFiltered = mySite.Lists[newListName]; //listFiltered is my newlist
SPListItemCollection newListItems = listFiltered.Items; //newListItem are the item from my list
foreach (SPListItem item in results)
{
//I don't know how to send my result to my SharePoint list :(
}
You will need to define your other List, then you can add a new SPListItem to that list with the columns that list contains. I am not sure what results is, if that is a typo or not, but I included that in my answer. You would need to change that if results does not exist.
SPList secondList = web.Lists["MyList"];
foreach(SPListItem item in results)
{
SPListItem Item = secondList.Items.Add();
item["Title"] = companyName
item["DateReceived"] = System.DateTime.Now;
item["Description"] = companyDesc;
item.Update();
}