Pages

Saturday, August 29, 2015

CAML Query with Person or Group Field in SharePoint

Querying By Display Name:-
Here is how to querying for Person or Group field by Display Name. Things that you have to concern here are Value Type is “User” and [User_Name] must be replaced with the Display Name (SPUser.Name) of the user.

Write the query as following:

<Where>
<Eq>
    <FieldRef Name='UserID' />
    <Value Type='User'>[User_Name]</Value>
</Eq>
</Where>

Querying By User ID:-
Advantage of querying by User ID is this will avoid failing of querying by Display Name when you have users who have same value for it. Here the most important thing to concern is that you have to add LookupId=”TRUE” property in FieldRef tag and also should replace [User_ID] with the ID (SPUser.ID) of the user.

Write the query as following:

<Where>
<Eq>
    <FieldRef Name='UserID' LookupId='TRUE'/>
    <Value Type='User'>[User_ID]</Value>
</Eq>
</Where>

Sunday, August 23, 2015

Use Multiple Conditions in CAML Queries

1. One Condition

<Where>      
            <Eq><FieldRef Name=’Name’ /> <Value Type=’Text’>Aasai</Value></Eq>
</Where>

2. Two Conditions

<Where>
          <And>
            <Eq><FieldRef Name=’Title’ />  <Value Type=’Text’>Mr</Value></Eq>
            <Eq><FieldRef Name=’Name’ /> <Value Type=’Text’>Aasai</Value></Eq>
         </And>
</Where> 

3. Three Conditions

<Where>
      <And>
         <And>
            <Eq><FieldRef Name=’Title’ />  <Value Type=’Text’>Mr</Value></Eq>
            <Eq><FieldRef Name=’Name’ /> <Value Type=’Text’>Aasai</Value></Eq>
         </And>
         <Eq><FieldRef Name=’Address’ /> <Value Type=’Text’>Chennai</Value></Eq>
      </And>
   </Where>

4. Four Conditions

<Where>
      <And>
         <And>
            <Eq><FieldRef Name=’Title’ />  <Value Type=’Text’>Mr</Value></Eq>
            <Eq><FieldRef Name=’Name’ /> <Value Type=’Text’>Aasai</Value></Eq>
         </And>
        <And>
         <Eq><FieldRef Name=’Address’ /> <Value Type=’Text’>Chennai</Value></Eq>
         <Eq><FieldRef Name=’Country’ /> <Value Type=’Text’>India</Value></Eq>
         </And>
      </And>
   </Where>

5. Multiple Conditions

<Where>
    <And>
        <Or>
            <Eq>
                <FieldRef Name='FirstName' />
                <Value Type='Text'>John</Value>
            </Eq>
            <Or>
                <Eq>
                    <FieldRef Name='LastName' />
                    <Value Type='Text'>John</Value>
                </Eq>
                <Eq>
                    <FieldRef Name='Profile' />
                    <Value Type='Text'>John</Value>
                </Eq>
            </Or>
        </Or>
        <And>       
            <Or>
                <Eq>
                    <FieldRef Name='FirstName' />
                    <Value Type='Text'>Doe</Value>
                </Eq>
                <Or>
                    <Eq>
                        <FieldRef Name='LastName' />
                        <Value Type='Text'>Doe</Value>
                    </Eq>
                    <Eq>
                        <FieldRef Name='Profile' />
                        <Value Type='Text'>Doe</Value>
                    </Eq>
                </Or>
            </Or>
            <Or>
                <Eq>
                    <FieldRef Name='FirstName' />
                    <Value Type='Text'>123</Value>
                </Eq>
                <Or>
                    <Eq>
                        <FieldRef Name='LastName' />
                        <Value Type='Text'>123</Value>
                    </Eq>
                    <Eq>
                        <FieldRef Name='Profile' />
                        <Value Type='Text'>123</Value>
                    </Eq>
                </Or>
            </Or>
        </And>
    </And>
</Where>

Sunday, August 16, 2015

Work with list items Using ECMA Script


This post going to describe that how to Insert, Update and Delete list items using ECMA Script(JavaScript Client Object Model).

Insert List Item

var nmspCon = {};

nmspCon.varEmpName = "";
nmspCon.varEmpAddress = "";
nmspCon.varEmpCity = "";
nmspCon.varEmpIdCardNo = "";

nmspCon.fncSaveForm = function () {

    nmspCon.varEmpName = $('#txtName').val();
    nmspCon.varEmpAddress = $('#txtAddress').val();
    nmspCon.varEmpCity = $('#txtCity').val();
    nmspCon.varEmpIdCardNo = $('#txtIdCardNo').val();

    nmspCon.clientContext = SP.ClientContext.get_current();
    nmspCon.oList = nmspCon.clientContext.get_web().get_lists().getByTitle('Employee Information');

    nmspCon.itemCreateInfo = new SP.ListItemCreationInformation();
    nmspCon.oListItem = nmspCon.oList.addItem(nmspCon.itemCreateInfo);

    nmspCon.oListItem.set_item('Name', nmspCon.varEmpName);
    nmspCon.oListItem.set_item('Address', nmspCon.varEmpAddress);
    nmspCon.oListItem.set_item('City', nmspCon.varEmpCity);
    nmspCon.oListItem.set_item('IdentityCardNumber', nmspCon.varEmpIdCardNo);

    nmspCon.oListItem.update();
    nmspCon.clientContext.load(nmspCon.oListItem);

    nmspCon.clientContext.executeQueryAsync(
    Function.createDelegate(this, nmspCon.onQuerySuccess),
    Function.createDelegate(this, nmspCon.onQueryFail)
    );
}

nmspCon.onQuerySuccess = function () {
                alert("Success.!");}

nmspCon.onQueryFail = function (sender, args) {
                alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());}



Update List Item

nmspCon.fncUpdateEmployeeInformation = function () {

    nmspCon.varType = "Temporary";

    nmspCon.clientContext = SP.ClientContext.get_current();
    nmspCon.oList = nmspCon.clientContext.get_web().get_lists().getByTitle('Employee Information');

    nmspCon.listItem = nmspCon.oList.getItemById(1);
    nmspCon.listItem.set_item('Type', '' + nmspCon.varType);
    nmspCon.listItem.update();

    nmspCon.clientContext.load(nmspCon.listItem);
    nmspCon.clientContext.executeQueryAsync(
        Function.createDelegate(this, nmspCon.onQuerySuccess),
        Function.createDelegate(this, nmspCon.onQueryFail));
}

nmspCon.onQuerySuccess = function () {
                alert("Success.!");}

nmspCon.onQueryFail = function () {
                alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());}




Delete List Item

nmspCon.fncDeleteEmployeeInformation = function () {

    nmspCon.clientContext = SP.ClientContext.get_current();
    nmspCon.oList = nmspCon.clientContext.get_web().get_lists().getByTitle('Employee Information');

    nmspCon.listItem = nmspCon.oList.getItemById(1);
    nmspCon.listItem.deleteObject();

    nmspCon.clientContext.load(nmspCon.listItem);
    nmspCon.clientContext.executeQueryAsync(
        Function.createDelegate(this, nmspCon.onQuerySuccess),
        Function.createDelegate(this, nmspCon.onQueryFail));
}


nmspCon.onQuerySuccess = function () {
                alert("Success.!");}

nmspCon.onQueryFail = function () {
                alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());}