2

I have a requirement to fetch a single document from a document set. I have 5 different custom content types that have document set as the base type. Each document set has numerous documents. I want to get a particular document from the document set using a CAML query. Please guide.

1

2 Answers 2

0

This should work

var query = new SPQuery();
query.RowLimit = 1;
query.Folder = documentSet.Folder;
query.Query = string.Format("<Where><Eq><FieldRef Name='ContentType' /><Value Type='Computed'>{0}</Value></Eq></Where>", "ContentTypeName");
var items = documentSet.ParentList.GetItems(query);
0

I tried with the answer mentioned by Daniel but was unable to get the result. I have been able to get a file with a particular name from the document set by the following approach:

  • Fetch all the items of the Document library
  • Check whether the item is a document or not
  • If it document set, check whether it has subfolders
  • When is does not have sub folders, fetch the File you required from the document set.

Please refer the code given below, which shall give you a better clarity on what I am trying to explain.

//checks whether list item is document set or not.
    public bool IsDocumentSet(SPListItem item)
    {
        bool documentSetItem = false;
        DocumentSet documentSet = null;
        if (null != item && IsFolder(item))
        {
            documentSet = DocumentSet.GetDocumentSet(item.Folder);
            if (null != documentSet)
                documentSetItem = true;
        }
        return documentSetItem;
    }

    //checks whether list item contains folder
    public bool IsFolder(SPListItem item)
    {
        if (item.Folder != null)
        {
            return true;
        }
        else
        {
            return false;
        }

    }

      SPFile excelFile = null;
        SPListItemCollection collListItems = docList.Items;
        foreach (SPListItem oListItem in collListItems)
        {
            if (IsDocumentSet(oListItem))
            {
                SPFolder documentsetFolder = oListItem.Folder;
                if (documentsetFolder.Name.Equals("kevin"))
                {
                    SPFileCollection filecoll = documentsetFolder.Files;
                    foreach (SPFile tempfile in filecoll)
                    {
                        if (tempfile.Name.Contains("File name that you are looking for"))
                        {
                            excelFile = tempfile;
                        }
                    }
                }

            }
        }

docList is the SPList object of the document library that you are wanting to query.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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