Skip to main content
added 5 characters in body
Source Link
Vadim Gremyachev
  • 42.6k
  • 3
  • 88
  • 169

It actually depends on which version of SharePoint is used.

SharePoint 2013

In SharePoint 2013 CSOM ListItem.Folder property gets a folder object that is associated with a folder item.

SharePoint 2010

In SharePoint 2010 CSOM Folder property is not exposed for ListItem object.

The following method could be used for retrieving Folder associated with ListItem:

/// <summary> /// Get Parent Folder for List Item /// </summary> /// <param name="listItem"></param> /// <returns></returns> private static Folder GetListItemFolder(ListItem listItem) { var folderUrl = (string)listItem["FileDirRef"]; var parentFolder = listItem.ParentList.ParentWeb.GetFolderByServerRelativeUrl(folderUrl); listItem.Context.Load(parentFolder); listItem.Context.ExecuteQuery(); return parentFolder; } 

Example:

using (var context = new ClientContext(webUrl)) { var list = context.Web.Lists.GetByTitle(listTitle); var items = list.GetItems(CamlQuery.CreateAllItemsQuery()); context.Load(items); context.ExecuteQuery(); foreach (var item in items) { var folder = GetListItemFolder(item); //get Folder Console.WriteLine(folder.Name); } } 

It actually depends on which version of SharePoint is used.

SharePoint 2013

In SharePoint 2013 CSOM ListItem.Folder property gets a folder object that is associated with a folder item.

SharePoint 2010

In SharePoint 2010 Folder property is not exposed for ListItem object.

The following method could be used for retrieving Folder associated with ListItem:

/// <summary> /// Get Parent Folder for List Item /// </summary> /// <param name="listItem"></param> /// <returns></returns> private static Folder GetListItemFolder(ListItem listItem) { var folderUrl = (string)listItem["FileDirRef"]; var parentFolder = listItem.ParentList.ParentWeb.GetFolderByServerRelativeUrl(folderUrl); listItem.Context.Load(parentFolder); listItem.Context.ExecuteQuery(); return parentFolder; } 

Example:

using (var context = new ClientContext(webUrl)) { var list = context.Web.Lists.GetByTitle(listTitle); var items = list.GetItems(CamlQuery.CreateAllItemsQuery()); context.Load(items); context.ExecuteQuery(); foreach (var item in items) { var folder = GetListItemFolder(item); //get Folder Console.WriteLine(folder.Name); } } 

It actually depends on which version of SharePoint is used.

SharePoint 2013

In SharePoint 2013 CSOM ListItem.Folder property gets a folder object that is associated with a folder item.

SharePoint 2010

In SharePoint 2010 CSOM Folder property is not exposed for ListItem object.

The following method could be used for retrieving Folder associated with ListItem:

/// <summary> /// Get Parent Folder for List Item /// </summary> /// <param name="listItem"></param> /// <returns></returns> private static Folder GetListItemFolder(ListItem listItem) { var folderUrl = (string)listItem["FileDirRef"]; var parentFolder = listItem.ParentList.ParentWeb.GetFolderByServerRelativeUrl(folderUrl); listItem.Context.Load(parentFolder); listItem.Context.ExecuteQuery(); return parentFolder; } 

Example:

using (var context = new ClientContext(webUrl)) { var list = context.Web.Lists.GetByTitle(listTitle); var items = list.GetItems(CamlQuery.CreateAllItemsQuery()); context.Load(items); context.ExecuteQuery(); foreach (var item in items) { var folder = GetListItemFolder(item); //get Folder Console.WriteLine(folder.Name); } } 
Source Link
Vadim Gremyachev
  • 42.6k
  • 3
  • 88
  • 169

It actually depends on which version of SharePoint is used.

SharePoint 2013

In SharePoint 2013 CSOM ListItem.Folder property gets a folder object that is associated with a folder item.

SharePoint 2010

In SharePoint 2010 Folder property is not exposed for ListItem object.

The following method could be used for retrieving Folder associated with ListItem:

/// <summary> /// Get Parent Folder for List Item /// </summary> /// <param name="listItem"></param> /// <returns></returns> private static Folder GetListItemFolder(ListItem listItem) { var folderUrl = (string)listItem["FileDirRef"]; var parentFolder = listItem.ParentList.ParentWeb.GetFolderByServerRelativeUrl(folderUrl); listItem.Context.Load(parentFolder); listItem.Context.ExecuteQuery(); return parentFolder; } 

Example:

using (var context = new ClientContext(webUrl)) { var list = context.Web.Lists.GetByTitle(listTitle); var items = list.GetItems(CamlQuery.CreateAllItemsQuery()); context.Load(items); context.ExecuteQuery(); foreach (var item in items) { var folder = GetListItemFolder(item); //get Folder Console.WriteLine(folder.Name); } }