0

If you right click on a sub folder in a library and select Copy Shortcut.

How do you parse or use the Copy Shortcut value to CAML query the a single folder and its sub folders?

Copy Shortcut value:

http://sharepoint.com/siteName/LibraryName/Forms/AllItems.aspx?RootFolder=%2FsiteName%2FLibrary%20Name%2FFolderOne%2FFolder2&FolderCTID=0x01200065B0917E61502A4CB259DB83E8745555&View={D471308F-B032-4973-B7C2-56F75A6C8B46}

I'm working with a Visual Studio workflow along with SharePoint 2010.

edit / update

This is what I ended up with:

string sCopyFrom = http://sharepoint.com/siteName/LibraryName/Forms/AllItems.aspx?RootFolder=%2FsiteName%2FLibrary%20Name%2FFolderOne%2FFolder2&FolderCTID=0x01200065B0917E61502A4CB259DB83E8745555&View={D471308F-B032-4973-B7C2-56F75A6C8B46}
        using (SPSite siteCollection = new SPSite(sCopyFrom))
        {
            using (SPWeb webSub = siteCollection.OpenWeb())
            {

                SPList copySource = webSub.GetListFromUrl(sCopyFrom);

                string folderPath = sCopyFrom.Split(new string[] { "RootFolder=" }, StringSplitOptions.None)[1].Split('&')[0].Trim().Replace("%2F", "/").Replace("%20", " ").ToString(); ;

                SPFolder targetFolder = webSub.GetFolder(folderPath);

                // Query to get items
                SPQuery query = new SPQuery();
                query.RowLimit = 500;                        
                query.ViewAttributes = "Scope=\"Recursive\"";
                query.Folder = copySource.ParentWeb.GetFolder(targetFolder.UniqueId);
                query.Query = "";

                do
                {
                    SPListItemCollection collListItems = copySource.GetItems(query);
                    foreach (SPListItem item in collListItems)
                    {
                        //work with item
                    }

                    query.ListItemCollectionPosition = collListItems.ListItemCollectionPosition;
                } while (query.ListItemCollectionPosition != null);

            }
        }
  • Any specific reason you want to use CAML? This can be done without it – Michael Colbs May 21 '14 at 21:24
  • Need to process all the items within a given folder and its children. Each folder could have a couple thousand docs. Thought using CAML to batch process would be the most efficient route. – Taco_Buffet May 22 '14 at 12:09
  • Once you have targetFolder you can get its sub folders by referencing its SubFolders collection. What is the point of the SPQuery? – Rob D'Oria May 22 '14 at 13:58
  • Using SPQuery I was able to get the target folder and the contents of the children folders. The other part of the workflow works with all the items in a library. Given the size of the libraries, SPQuery and batch processing is leveraged. Basically, I needed the ability to process items at the library level or folder level. – Taco_Buffet May 22 '14 at 17:46
  • Unfortunately, you didn't put anything past the 1st part of that in your original question. – Rob D'Oria May 22 '14 at 21:29
1

You don't need CAML. Try this:

    string url = "http://sharepoint.com/siteName/Library Name/Forms/AllItems.aspx?RootFolder=%2FsiteName%2FLibrary%20Name%2FFolderOne%2FFolder2&FolderCTID=0x01200065B0917E61502A4CB259DB83E8745555&View={D471308F-B032-4973-B7C2-56F75A6C8B46}";
    string siteUrl = new Uri(url).GetLeftPart(UriPartial.Path);
    string serverRelativeFolderUrl = HttpUtility.ParseQueryString(url)["RootFolder"];
    using (SPSite site = new SPSite(siteUrl))
    {
        using (SPWeb web = site.OpenWeb())
        {
            SPFolder folder = web.GetFolder(serverRelativeFolderUrl);
            foreach (SPFolder subFolder in folder.SubFolders)
            {
                // do whatever
            }
        }
    }

The RootFolder argument on the querystring the server relative URL for the Folder, which is what SPWeb.GetFolder takes as its parameter. You can do whatever you want with the folder object from there, including enumerating its sub folders.

  • It was my understanding that you can't use SPContext within a SharePoint workflow. Is this true? – Taco_Buffet May 22 '14 at 11:58
  • That's true...didn't pick up on that in your question when I read it the 1st time. General approach still works, just need to construct SPSite object from URL. I updated the code to reflect this. – Rob D'Oria May 22 '14 at 12:29
0

In your CAML you'd want to have <QueryOptions><ViewAttributes Scope='RecursiveAll' /></QueryOptions>

This will look for all files in all folders.

  • How do you pass the URL that I provided into the query? – Taco_Buffet May 22 '14 at 11:58
0

If you want to target a single folder instead of getting all items, try this:

SPFolder oFolderParent = oWeb.Folders[strDocLibName];
//Get Reference to the subfolder
SPFolder oFolderChild = oFolderParent.SubFolders[albumName];
SPDocumentLibrary oDocumentLibrary = (SPDocumentLibrary)oWeb.Lists[strDocLibName];
SPView oView = oDocumentLibrary.Views["Thumbnails"];
//Get all the photo SPListItems in this subfolder
SPListItemCollection collListItems = oDocumentLibrary.GetItemsInFolder(oView, oFolderChild);
SPListItem item;
for (int intIndex = 0; intIndex < collListItems.Count; intIndex++)
{
  item = collListItems[intIndex];
  colImages.Add(item.File.ServerRelativeUrl);
}

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.