Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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

Any sample code available?

share|improve this question
add comment

2 Answers

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
add comment

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
add comment

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.