0

I want to pass the spuser as a paramter to the caml query

but when i tried the below code its throwing error:

        using (SPWeb spWeb = spSite.OpenWeb())
                {
                    SPDocumentLibrary objDocLibProjDocLibrary = 
         spWeb.Lists.TryGetList("ProjectDocumentLibrary") as 
       SPDocumentLibrary;

                   // string strsingleProjCodeStageTupleSet = 
          "VGIL_NPR_TBD_STB_050_2017"; 
         //"VGIL_NPR_EMD_STB_993_2017";//"VGIL_NPR_EMD_STB_999_2017"; 

                    string strstage1 = "Concept And Design Proto 
          Developement";//"Feasibility and Kick Off"; //
                    string strnp11user =@"mydomain\NPD11";


                    string strsingleProjCodeforptp = 
           "VGIL_NPR_TBD_STB_050_2017";
                    SPUser objuser1 = spWeb.EnsureUser(strnp11user);

                    //objuser1.LoginName

                   string  strPTPQueryforPDL = "<Where><And><Eq>"
                         + "<FieldRef Name='ProjectCode' />"
           Value Type='Text'>" + strsingleProjCodeforptp + "</Value>"
                        + "</Eq><And><Eq><FieldRef Name='AssignedTo' />"
                       + "<Value Type='User'>" + objuser1.LoginName + "
        </Value></Eq><Or><IsNull>"
                       + "<FieldRef Name='TriggerFromTimerJob' />"
                       + "</IsNull><Eq>"
                       + "<FieldRef Name='TriggerFromTimerJob' /><Value 
          Type='Text'>NO</Value>"
                       + "</Eq></Or></And></And></Where>";
                    SPQuery oQueryProjDocLibCodeStage = new SPQuery();
                    SPListItemCollection 
              oListItemCollProjDocLibProjCodeStage;
                    oQueryProjDocLibCodeStage.Query = strPTPQueryforPDL;
                    oListItemCollProjDocLibProjCodeStage = 
       objDocLibProjDocLibrary.GetItems(oQueryProjDocLibCodeStage);

                    int counterprojdoclibstage = 
       oListItemCollProjDocLibProjCodeStage.Count;

        if (oListItemCollProjDocLibProjCodeStage != null && 
       oListItemCollProjDocLibProjCodeStage.Count > 0)
                    {
                        foreach (SPListItem singleProjDocLibCodeStageItem in 
            oListItemCollProjDocLibProjCodeStage)
                        {
                            int idofmatchingprojdoclib = 
           Convert.ToInt32(singleProjDocLibCodeStageItem["ID"]);

                        }
                    }

it seems , "" + "" + objuser1.LoginName + "" line of query is throwing error.

in order to get the correct items after caml query execution, what should be written ? i cant hardcode the spuser values

am using NTLM authentication . i tried with the below query as well:

     string  strPTPQueryforPDL = "<Where><And><Eq>"
                         + "<FieldRef Name='ProjectCode' />"
                        + "<Value Type='Text'>" + strsingleProjCodeforptp + 
                  "</Value>"
                        + "</Eq><And><Eq><FieldRef Name='AssignedTo' />"
                       + "<Value Type='Integer'><UserID>" + objuser1.ID + "
                 </UserID></Value></Eq><Or><IsNull>"
                       + "<FieldRef Name='TriggerFromTimerJob' />"
                       + "</IsNull><Eq>"
                       + "<FieldRef Name='TriggerFromTimerJob' /><Value  
           Type='Text'>NO</Value>"
                       + "</Eq></Or></And></And></Where>";

                   that also failed!

            <Eq><FieldRef Name='AssignedTo' />"
      + "<Value Type='User' LookupId='True'>>" + objuser1.Id+ "</Value></Eq>

also didnt work Which one is the correct caml query to get the actual result count?

2 Answers 2

2

You forget to add LookupId="True" for the field in CAML Query,

  <Eq>
    <FieldRef Name="AssignedTo" LookupId="True" />
    <Value Type="User">//userid here ex. objuser1.Id</Value>
  </Eq>

By following ways you can make query on user,

  • If you know the display name of the user

    <Where>
     <Eq>
        <FieldRef Name='User_x0020_ID' />
       <Value Type='User'>System Account</Value>
    </Eq>
    </Where>
    
  • If you know the User Id ( not the login id)

      <Where>
        <Eq>
        <FieldRef Name='User_x0020_ID' LookupId='True'  />
        <Value Type='User'>1073741823</Value>
        </Eq>
     </Where>
    
  • If you would like to filter based on current user (logged in User )

     <Where>
     <Eq>
       <FieldRef Name='User_x0020_ID'/>
       <Value Type='User'><UserID/></Value>
    </Eq>
    </Where>
    

you can visit this.

0

try like this Instead of 1 pass the userid

 <Where>
     <Eq>
      <FieldRef Name='User' LookupId='TRUE' />
       <Value Type='Lookup'>1</Value>
     </Eq>
    </Where>

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.