Skip to main content
Missing "item" in code
Source Link
Thomas
  • 980
  • 3
  • 10
  • 26

Is it possible to use some built in SharePoint API to open a file no matter which web and library it's in?

Example:

Given the file: /subsiteA/PublishingImages/myimage.jpg I want to open it and get the list item, but using this fails:

var url = "/subsiteA/PublishingImages/myimage.jpg";
var file = SPContext.Current.Site.RootWeb.GetFile(url);
SPListItem item = file.GetListItem(); //Fails here with item not in library error

Naturally it will not work as the file is not located in the root web, but is there a generic way to locate the file no matter which web and library it's in and retrieve the SPListItem object related to the file?

Is it possible to use some built in SharePoint API to open a file no matter which web and library it's in?

Example:

Given the file: /subsiteA/PublishingImages/myimage.jpg I want to open it and get the list item, but using this fails:

var url = "/subsiteA/PublishingImages/myimage.jpg";
var file = SPContext.Current.Site.RootWeb.GetFile(url);
SPListItem = file.GetListItem(); //Fails here with item not in library error

Naturally it will not work as the file is not located in the root web, but is there a generic way to locate the file no matter which web and library it's in and retrieve the SPListItem object related to the file?

Is it possible to use some built in SharePoint API to open a file no matter which web and library it's in?

Example:

Given the file: /subsiteA/PublishingImages/myimage.jpg I want to open it and get the list item, but using this fails:

var url = "/subsiteA/PublishingImages/myimage.jpg";
var file = SPContext.Current.Site.RootWeb.GetFile(url);
SPListItem item = file.GetListItem(); //Fails here with item not in library error

Naturally it will not work as the file is not located in the root web, but is there a generic way to locate the file no matter which web and library it's in and retrieve the SPListItem object related to the file?

Source Link
Thomas
  • 980
  • 3
  • 10
  • 26

Get file from any library in the web tree

Is it possible to use some built in SharePoint API to open a file no matter which web and library it's in?

Example:

Given the file: /subsiteA/PublishingImages/myimage.jpg I want to open it and get the list item, but using this fails:

var url = "/subsiteA/PublishingImages/myimage.jpg";
var file = SPContext.Current.Site.RootWeb.GetFile(url);
SPListItem = file.GetListItem(); //Fails here with item not in library error

Naturally it will not work as the file is not located in the root web, but is there a generic way to locate the file no matter which web and library it's in and retrieve the SPListItem object related to the file?