?
Solved

Sharepoint 2010 Client object model

Posted on 2012-09-21
6
Medium Priority
?
2,249 Views
Last Modified: 2012-09-21
is it possible to update a file in document library (i mean replace with a new file). i don't want to delete the record and recreate again since i will loose all the meta data associated with that file. if possible can you please direct me in right direction or if possible can anyone provide code snippet(client object model)
0
Comment
Question by:ishancognite
  • 3
  • 3
6 Comments
 
LVL 11

Assisted Solution

by:svetaye
svetaye earned 2000 total points
ID: 38421287
Hello,
You actually can overwrite the file and it will save the metadata is you are using versions in your document library (it will create a new version of the file and save the metadata).

Please check this code for uploading a file:
http://blogs.msdn.com/b/sridhara/archive/2010/03/12/uploading-files-using-client-object-model-in-sharepoint-2010.aspx

And this thread for the code snippet to update a metadata :
http://stackoverflow.com/questions/6947491/updating-field-value-in-sharepoint-using-client-object-model


I hope it helps.
0
 

Author Comment

by:ishancognite
ID: 38421301
Hi
thanks for you comment it helped me a lot. but i have a small issue. after i overwrite the file can i update the file name, if possible can you please direct me for possible solution
Thanks for your help
0
 
LVL 11

Accepted Solution

by:
svetaye earned 2000 total points
ID: 38421409
Here is the code to rename a file:

            ClientContext ctx = new ClientContext("hostURL");
            List list = ctx.Web.Lists.GetByTitle("LibraryName");
            ListItemCollection items = list.GetItems(CamlQuery.CreateAllItemsQuery());
            ctx.Load(items); // loading all the fields
            ctx.ExecuteQuery();

            items[0]["FileLeafRef"] = "Sveta's Document";  
            items[0].Update(); 
            ctx.ExecuteQuery();

Open in new window


Change  items[0] to your item you want to rename. You can find it in the list by ID or by previous name or using a CAML Query.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:ishancognite
ID: 38421422
thanks . when i try to update the file name it is coming up with version conflict error message any idea or how to resolve the issue.
0
 
LVL 11

Assisted Solution

by:svetaye
svetaye earned 2000 total points
ID: 38421456
Are you trying to update it just after the uploading? Try  to upload a file ,then create a new ListItem object, find your item in the library and then change the name. Maybe the upload process still running when you are trying to rename the file.
0
 

Author Comment

by:ishancognite
ID: 38421809
yes you are correct i managed to fix the issue .thanks for your help
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

SharePoint Designer 2010 has tools and commands to do everything that can be done with web parts in the browser, and then some – except uploading a web part straight into a page that is edited in SPD. So, can it be done? Scenario For a recent pr…
We had a requirement to extract data from a SharePoint 2010 Customer List into a CSV file and then place the CSV file into a directory on the network so that the file could be consumed by an AS400 system. I will share in Part 1 how to Extract the Da…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?

755 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question