4

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

Any sample code available?

2

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);
| improve this answer | |
2

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);
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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