I'm trying to write a CamlQuery that goes through all of the documents in the library and returns a list of docs that contain certain metadata tags.

I have a working version which recursively does this but it takes forever. However my Visual Studio project doesn't seem to notice the using Microsoft.SharePoint.Client; import and therefore cannot find the CamlQuery reference.

Is there some other place I need to include this for it to be recognized?


using System;
using Microsoft.SharePoint.Client;
public content getTopicDocs2(string meta)
    content returnContent = new content();
    List<doc> ReturnItems = new List<doc>();

    SPWeb web = SPContext.Current.Web;
    SPDocumentLibrary lib = (SPDocumentLibrary)web.Lists["public"];

    CamlQuery query = new CamlQuery();
    query.ViewXml = "<View><Query><Where><Contains><FieldRef Name='Topics'/><Value Type='Text'>" + meta + "</Value></Contains></Where></Query></View>";
    ListItemCollection collListItem = lib.GetItems(query);


The type or namespace 'CamlQuery' could not be found

  • try by removing <view></view> and <query></query>, only keep <where></where >. – karthik Oct 9 '17 at 18:42
  • @karthik the error is at CamlQuery query = new CamlQuery(); – PT_C Oct 9 '17 at 18:51

First, The above code is Server-side object model, not Client Side object model so you should use Microsoft.SharePoint not Microsoft.SharePoint.Client namespace

Second, the CamlQuery is used with Client Side object model at Microsoft.SharePoint.Client namespace.

Third, in Server-side object model you should use SPQuery with Microsoft.SharePoint

Fourth, you can't use SSOM with CSOM!

Fifth, the above code has a lot of error and will not work !!

The Last thing, to get items from doc library via SSOM check

using (SPSite site = new SPSite("http://mySite/"))
    using (SPWeb web = site.OpenWeb())
        SPQuery query = new SPQuery();
        query.Query = "<View><Query><Where><Contains><FieldRef Name='Topics'/><Value Type='Text'>" + meta + "</Value></Contains></Where></Query></View>";
        SPList docLib = web.Lists["Shared Documents"];    
        SPListItemCollection files = docLib .GetItems(query);
              try by removing CAMl Query query= new CAMLQuery(); and query in this way                                                                                                              
SPQuery Query= new SPQuery();                                                                              
string qry=@"yourcaml query   

SPQuery query  =new SPQuery();                                                                   
query.Query = string.concat("<Where><Contains><FieldRefName='Topics'/<ValueType='Text'>" + meta + "</Value></Contains</Where>"); 

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.