4

I have a utility function to copy documents from one folder to another folder in SharePoint Online using C#/CSOM. It works well for subfolders, but I am having issues with RootFolders for libraries.

For example, this will get the root list for a subfolder. I use the return List for other tasks like copying content types, fields, and other configuration for the copied documents:

public static List Foo(Folder bar)
{
     return bar.ListItemAllFields.ParentList;
}

Problem is, when I pass a List.RootFolder object to this function (which uses the Folder class), there is an error because ListItemAllFields is null. The question is how do I get the parent list with just the Folder as a parameter if the folder's ListItemAllFields property is null? This does not work:

public static void FooBar(List list)
{
     List self = Foo(list.RootFolder); 
}
public static List Foo(Folder bar)
{
     return bar.ListItemAllFields.ParentList;
}

Thanks

1

I have written utility for SPO site to copy entire library folders and files. The syntax I am using as follows

List srcLst = srcCtx.Web.Lists.GetByTitle(SourceListTitle);
var srcLibRootFolder = srcLst.RootFolder;
srcCtx.Load(srcLibRootFolder);
srcCtx.ExecuteQuery();

After Load operation, I am getting root folder object.

1

If you have the URL of the folder, you can use Web.GetList(), and pass the url in:

var list = ctx.Web.GetList(folder.ServerRelativeUrl);

Not sure if this is viable in your scenario, but it's one option.

MSDN documentation for Web.GetList().

  • Thanks, I appreciate the response. The way I have it set up is to not accept Web as a parameter. I am doing this so I can re-use the function more easily. I tried doing var ctx = (ClientContext)folder.Context; List list = ctx.Web.GetListByUrl(url), but folder.Context could reference a context created for the root web of the site collection or another parent web. Therefore, ClientContext.Web may not actually refer to the same Web as the folder. – Kevin Stone May 31 '17 at 18:54
  • I'm not seeing a way to get a List instance from the root folder. I can see ways to obtain the list, if you have a ClientContext. The way above is one such way, another would be to use the property bag (you can get the list id and name there). Sorry I'm unable to help – wjervis Jun 1 '17 at 10:52

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.