How to set metadata info when you use savebinarydirect() in client object model?
-
Wednesday, October 05, 2011 1:47 PM
Hi ,
i want to upload files in document library in client object model (COM). i used FileCollection.Add() to upload the files. but problem with this method is it wont upload files which are larger than 3mb. Then I got to know that if you use SaveBinaryDirect() you can upload files of any size (ofcourse you need to add some more code with it ). But my doubt now is , if I go with SaveBinaryDirect() how can I edit the metadata information? will it be possible? (I need to edit the metadatainfo such as createdby , modified by etc using code because of some reasons)
I used the below code to edit the metadata info when I used FileCollection.Add()
File file = lst.RootFolder.Files.Add(fileCreationInfo); clientContext.Load(file); ListItem item = file.ListItemAllFields; clientContext.Load(item); User user = clientContext.Web.EnsureUser(UserId); clientContext.Load(user); clientContext.ExecuteQuery(); item["Author"] = user; item["Editor"] = user; Ffile.ListItemAllFields.Update(); clientContext.ExecuteQuery();
Thanks and Regards,
Prajith.
All Replies
-
Friday, October 07, 2011 6:10 AM
Hi PraTech,
Thanks for your post.
Just as other type column, you can set the metadata column using client object model. But you should know the term id at first.
Sample code:
string termValue = termItem["ID"] + ";#" + termItem["Term"] + "|" + termItem["IdForTerm"];
item["Cars"] = termValue;
item["CarsTaxHTField0"] = termValue
- Proposed As Answer by Ravi S Kulkarni Friday, October 07, 2011 6:53 AM
-
Friday, October 07, 2011 6:53 AM
Hey,
public static void GetTermSetInfo(Field field) { string sspId = string.Empty; string termSetId = string.Empty; XElement schemaRoot = XElement.Parse(field.SchemaXml); foreach (XElement property in schemaRoot.Descendants("Property")) { XElement name = property.Element("Name"); XElement value = property.Element("Value"); if (name != null && value != null) { switch (name.Value) { case "SspId": sspId = value.Value; break; case "TermSetId": termSetId = value.Value; break; } } } }
A managed metadata column can only be tied to one termset.You can get the field's sharedServiceId and termSetId from its schema using the client OM.
"The Only Way To Get Smarter Is By Playing A Smarter Opponent" -
Sunday, October 09, 2011 5:44 PM
Once you have uploaded the file, just find that item/document/file in the document library using the fileleafref column. Then modify the metadata of the item as shown in the code below:
Web web = context.Web;
List list = web.Lists.GetByTitle(libraryName);
ListItemCollection itemCol = list.GetItems(new CamlQuery() { ViewXml = "<View/>" });
context.Load(itemCol,
items => items
.Include(i => i[COLUMN_IMPORTORDERNUMBER])
.Where(i => (string)i[COLUMN_FILELEAFREF] == filename)
.Take(1)
);
context.ExecuteQuery();
if (itemCol != null && itemCol.Count > 0)
{
ListItem item = itemCol[0];
item[COLUMN_ToBEChanged] = "Test data";
item.Update(); // check in the item
item.File.CheckIn("Checked in by WebService", CheckinType.MajorCheckIn);
context.ExecuteQuery();
}This code comes from: http://stackoverflow.com/questions/3609778/how-to-upload-a-file-to-a-sharepoint-library-via-one-of-the-standard-webservices
-
Tuesday, October 11, 2011 5:27 AM
Thanks for your replies guyz.
But I am sorry to say that I didnt get things working may be because I am not clear with the above mentioned answers.Can anyone please simplify the things?
Let me tell you once again what exactly I needed .
ClientContext ctx = new ClientContext ("http://myserver:5656/MyWeb"); System.IO.FileInfo f = new System.IO.FileInfo (@"D:\TestDoc.txt"); System.IO.FileStream fs = f.OpenRead(); File.SaveBinaryDirect(ctx, "/MyWeb/SharedDocuments /Folder1/TestDoc.docx", fs, true);Above code is working fine. so the file is uploaded successfully under Folder1. Now how can I set the "Author" and "Editor" field programatically?
Thanks and Regards,
PraTech
-
Tuesday, October 11, 2011 7:35 AM
Hi Pratech,
Once you have uploaded your file using File.SaveBinaryDirect method, you will have to get the handle of the file you uploaded. You can use the below code to get the handle
Microsoft.SharePoint.Client.File file= web.GetFileByServerRelativeUrl("Your file Url"); ListItem lstItem = file.ListItemAllFields; clientContext.Load(lstItem); clientContext.ExecuteQuery();Once you have got the list handle, you can update the editor field as below
lstItem["Editor"] = web.EnsureUser("Login Name"); lstItem.Update(); clientContext.ExecuteQuery();
I am not sure if we can update the author field using client object model.
- Edited by M Rakesh Jain Tuesday, October 11, 2011 7:36 AM Modified Text
- Marked As Answer by PraTech Tuesday, October 11, 2011 8:42 AM
-
Tuesday, October 11, 2011 8:42 AM
Wonderful one Rakesh!!
What a simple way !! Congratz ! Its working . Thanks a lot.
Regards,
PraTech.
-
Monday, November 28, 2011 6:02 AM
Hi,
I followed the same steps but in
clientContext.ExecuteQuery(); i got an error
The object specified does not belong to a list.and it will exit from this point. What can be the reason for this?Thanks in Advance -
Thursday, December 01, 2011 4:58 AM
Hi Mukesh,
Can you please post your code here?
Thanks and Regards,
Prajith
-
Friday, March 23, 2012 8:11 AM
I wrote an article that I think it may help you so check this out:
http://ridha-temala.blogspot.com/2012/03/update-created-by-author-modified.html

