5

I have a large document library (at the moment ~6000 documents) and I need to find a document based on a custom field value (custom column on the library).

Is there a way of getting this document back without iterating through all 6000 documents?

I understand that an iteration must occur at some point, but I would prefer it to happen on the SharePoint server side, rather than transfer them all to the client side then cherry pick the document.

Thanks

Russell
  • 17,481
  • 23
  • 81
  • 125

1 Answers1

3

You can query Sharepoint. You issue a CAML query which is executed on the server and brings back only items that match the criteria that you specified. You specify the name of the custom column to search on and you specify the value to find. For efficiency , you can ask only for a few fields back (document url for example). So, you do not need to iterate over documents in the list to find the item.

You can find some discussion here: http://msdn.microsoft.com/en-us/library/ee956524.aspx and you can also find examples how to do it from javascript or silvelight.

Example CAML:

        CamlQuery camlQuery = new CamlQuery();
    camlQuery.ViewXml =
        @"<View>
            <Query>
              <Where>
                <Eq>
                  <FieldRef Name='FileLeafRef'/>
                  <Value Type='Text'>Test.docx</Value>
                </Eq>
              </Where>
              <RowLimit>1</RowLimit>
            </Query>
          </View>";
KJRB
  • 356
  • 1
  • 3
  • That Caml query returns the document by filename though. How do you query by the Document ID? I have already tried Document ID, Document ID Value, etc with no success... – Russell Jul 04 '11 at 03:42
  • If this is Sharepoint's ID field it's name would be "ID", If this is your custom field name than it will be very likely be "Document_x0020_ID". Sharepoint replaces spaces in field names with x0020. Also, Sharepoint truncates the field name if it's too long and you also need to know the type of the field. If it's built-in "ID" field it's type is Counter for example: 123. If your field is a custom text field: xxxx should work. – KJRB Jul 04 '11 at 21:24
  • If the above suggestion will not work (maybe the original field name was set to something different than Document ID) you will need to find out what is the internal field name for your "Document ID" column. One way to see it is to look at the url of the site column in question. You should see an internal name in the url of that page (field=). The Display Name of the field is not the same as interanl name. CAML queries use internal names. – KJRB Jul 05 '11 at 01:12
  • Yeah I'm talking about Sharepoints internal Document ID. I'll give the plain ID field a go, cheers. – Russell Jul 05 '11 at 02:04
  • The ID field returns the GUID, not the document ID. – Russell Jul 05 '11 at 04:03
  • I hope that you solved your problem already .. but if not I think that it would help if you specify what is: "Document ID". "ID" should be an integer, UniqueID is a Guid. Both are built in sharepoint fields. Can you define "Document ID" .. if this is a custom field or a built in field (I do not think it's a built in). – KJRB Jul 08 '11 at 04:27
  • I am talking about the Document ID introduced in SharePoint 2010: http://blogs.technet.com/b/blairb/archive/2009/10/20/new-document-id-feature-in-sharepoint-2010.aspx – Russell Jul 08 '11 at 05:26
  • The internal name for that field should be: _dlc_DocId. This works for me: @" xxx 100 "; More information: http://msdn.microsoft.com/en-us/library/ee559302.aspx. I am quite sure that the name is correct (no errors) – KJRB Jul 14 '11 at 03:26