SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Is it possible to get the GUID if I have the path to a file?

Any sample code available?

share|improve this question

You have to use the ClientContext.Web.GetFileByServerRelativeUrl() method.

Here is a sample Code:

ClientContext ctx = new ClientContext("http://contoso");

ctx.ExecuteQuery();

List list = ctx.Web.Lists.GetByTitle("Asset Library");

File file = ctx.Web.GetFileByServerRelativeUrl("/Asset Library/filename.png");

ctx.Load(file);

ctx.ExecuteQuery();

ListItem item = file.ListItemAllFields;

ctx.Load(item);

ctx.ExecuteQuery();

string idAsString = item["UniqueId"].ToString();

Guid id = new Guid(idAsString);
share|improve this answer

ashwnacharya's solution is correct. but you can optimize it, such that only one query execution need :

ClientContext ctx = new ClientContext("http://contoso");

ctx.ExecuteQuery();

List list = ctx.Web.Lists.GetByTitle("Asset Library");

File file = ctx.Web.GetFileByServerRelativeUrl("/Asset Library/filename.png");

ListItem item = file.ListItemAllFields;

ctx.Load(item);

ctx.ExecuteQuery();

string idAsString = item["UniqueId"].ToString();

Guid id = new Guid(idAsString);
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.