Prasad Bolla's SharePoint Blog

Click Here to go through the Interesting posts within my Blog.

Click Here to go through the new posts in my blog.

Thursday, January 31, 2013

Binding Documents to a Div using JQuery And SharePoint Default Web Services

 Please go to this post to understand what  JS files you need to download to achieve the required functionality.

var strSiteUrl = "http://SPSERVER:100";
var objList = new SPAPI_Lists(strSiteUrl);
var strHTML = "";

function BindSharedDocuments()
{
    var sQuery = "<Query><OrderBy><FieldRef Name='ID' Ascending='False' /></OrderBy></Query>";
    var strViewFields = "<ViewFields><FieldRef Name='BaseName' /><FieldRef Name='EncodedAbsUrl' /></ViewFields>";
    var result = objList.getListItems("UserPages", "", sQuery, strViewFields, "", "<QueryOptions><ViewAttributes Scope='Recursive' /></QueryOptions>", "");
     var ItemRows = '';
    if ($.browser.webkit)
    {
        ItemRows = "row";
    }
    else
    {
        ItemRows = "z\\:row";
    }
       strHTML+="<Table width='100%' cellpadding='0' cellspacing='0'>";
       strHTML+="<Tr>";
    strHTML+="<Td>";
    strHTML+="Document Name";
    strHTML+="</Td>";
    strHTML+="</Tr>";
    alert($(result.responseXML).find(ItemRows).length);
    if ($(result.responseXML).find(ItemRows).length > 0)
    {
        $(result.responseXML).find(ItemRows).each(function ()
        {
            strHTML+="<Tr>";
            strHTML+="<Td>";
            strHTML+="<A href='"+$(this).attr('ows_EncodedAbsUrl')+"'>"+$(this).attr('ows_BaseName')+"</a>";
            strHTML+="</Td>";
            strHTML+="</Tr>";
        });
    }
    strHTML+="</Table>";
    $("#divSharedDocuments").html(strHTML);
}

$(document).ready(function()
{
    BindSharedDocuments()

});

Check Whether the Logged User is Admin or Not using JQuery & SharePoint Default WebServices


1. First Place one Login Control within a Master Page and Take IT's ID from the View Source of the Page.
2. Download the SPAPI_Core.JS File
3. Download the SPAPLI_UserGroup.JS File

var strSiteUrl="http://SPSERVER:100";
var objGroup=new SPAPI_UserGroup(strSiteUrl);


function checkAdminUser()
{
   var strValidUser=false;
    var strCurrentUser=$("#ctl00_lgnUser").html();
    var ug1 = objGroup.getGroupCollectionFromUser(strCurrentUser);
    $(ug1.responseXML).find("Group").each(function()
    {
         if($(this).attr("ID")==3)
         {
             strValidUser=true;
         }
   
    });
   
    if(strValidUser==true)
    {
        alert('The User Logged is Admin User');
    }
}

$(document).ready(function()
{
    checkAdminUser();
});

Delete a Single Record using JQuery And SharePoint Default WebServices

Please go to this post to understand what  JS files you need to download to achieve the required functionality.
 
var strSiteUrl = "http://SPSERVER:100";
var objList = new SPAPI_Lists(strSiteUrl);

var strItemID=4;
var oWs= objList.quickDeleteListItem('JWS Practice',strItemID);
    if(oWs.status==200)
    {
        alert('Item Deleted Successfully');

    }

Updating a Single Record using JQuery and SharePoint Default WebServices

Please go to this post to understand what  JS files you need to download to achieve the required functionality.
 
var strSiteUrl = "http://SPSERVER:100";
var objList = new SPAPI_Lists(strSiteUrl);

var oWs = objList.quickUpdateListItem('JWS Practice', [{Title:("Single Update"),Country:("1"),Gender:("Male"),ID:("4")}]);
    if(oWs.status==200)
    {
        alert('Item Updated Successfully');

    }
 

Deleting More Than One Record using Batch in JQuery And SharePoint Default WebServices

Please go to this post to understand what  JS files you need to download to achieve the required functionality.
 
var strSiteUrl = "http://SPSERVER:100";
var objList = new SPAPI_Lists(strSiteUrl);

var strBatch="";
    strBatch+="<Batch PreCalc='TRUE' OnError='Continue'>";
    strBatch+="<Method ID='1' Cmd='Delete'>";
    strBatch+="<Field Name='ID'>1</Field>";
    strBatch+="</Method>";
    strBatch+="<Method ID='2' Cmd='Delete'>";
    strBatch+="<Field Name='ID'>2</Field>";
    strBatch+="</Method>";
    strBatch+="<Method ID='3' Cmd='Delete'>";
    strBatch+="<Field Name='ID'>3</Field>";
    strBatch+="</Method>";
    strBatch+="</Batch>";
   
     var res=objList.updateListItems("JWS Practice",strBatch);
     if(res.status==200)
     {
        alert('Records Deleted Successfully');
     }
 

Updating More Than one Record using Batch in Jquery And SharePoint Default WebServices

Please go to this post to understand what  JS files you need to download to achieve the required functionality.
 
var strSiteUrl = "http://SPSERVER:100";
var objList = new SPAPI_Lists(strSiteUrl);

var strBatch="";
    strBatch+="<Batch PreCalc='TRUE' OnError='Continue'>";
    strBatch+="<Method ID='1' Cmd='Update'>";
    strBatch+="<Field Name='ID'>1</Field>";
    strBatch+="<Field Name='Title'>Sample Batch Testing1</Field>";
    strBatch+="<Field Name='Country'>1</Field>";
    strBatch+="<Field Name='Gender'>Male</Field>";
    strBatch+="</Method>";
    strBatch+="<Method ID='2' Cmd='Update'>";
    strBatch+="<Field Name='ID'>2</Field>";
    strBatch+="<Field Name='Title'>Sample Batch Testing2</Field>";
    strBatch+="<Field Name='Country'>2</Field>";
    strBatch+="<Field Name='Gender'>Female</Field>";
    strBatch+="</Method>";
    strBatch+="<Method ID='3' Cmd='Update'>";
    strBatch+="<Field Name='ID'>3</Field>";
    strBatch+="<Field Name='Title'>Sample Batch Testing3</Field>";
    strBatch+="<Field Name='Country'>3</Field>";
    strBatch+="<Field Name='Gender'>Male</Field>";
    strBatch+="</Method>";
    strBatch+="</Batch>";
   
     var res=objList.updateListItems("JWS Practice",strBatch);
     if(res.status==200)
     {
        alert('Records Updated Successfully');
     }

Inserting More Than one record using batch in JQuery and SharePoint Default Web Services

Please go to this post to understand what  JS files you need to download to achieve the required functionality.



var strSiteUrl = "http://SPSERVER:100";
var objList = new SPAPI_Lists(strSiteUrl);



var strBatch="";

                strBatch+="<Batch PreCalc='TRUE' OnError='Continue'>";
                strBatch+="<Method ID='1' Cmd='New'>";
                strBatch+="<Field Name='ID'>New</Field>";
                strBatch+="<Field Name='Title'>Sample Batch Testing1</Field>";
                strBatch+="<Field Name='Country'>1</Field>";
                strBatch+="<Field Name='Gender'>Male</Field>";
                strBatch+="</Method>";
                strBatch+="<Method ID='2' Cmd='New'>";
                strBatch+="<Field Name='ID'>New</Field>";
                strBatch+="<Field Name='Title'>Sample Batch Testing2</Field>";
                strBatch+="<Field Name='Country'>2</Field>";
                strBatch+="<Field Name='Gender'>Female</Field>";
                strBatch+="</Method>";
                strBatch+="<Method ID='3' Cmd='New'>";
                strBatch+="<Field Name='ID'>New</Field>";
                strBatch+="<Field Name='Title'>Sample Batch Testing3</Field>";
                strBatch+="<Field Name='Country'>3</Field>";
                strBatch+="<Field Name='Gender'>Male</Field>";
                strBatch+="</Method>";
                strBatch+="</Batch>";
               
                 var res=objList.updateListItems("JWS Practice",strBatch);
                 if(res.status==200)
                 {
                                alert('Record Saved Successfully');
                 }
 

Insertion into a SharePoint List using JQuery and SharePoint Web Services

Please go to this post to understand what  JS files you need to download to achieve the required functionality.


Note:-
1.       Shown how to insert a data into a Lookup Column
2.       Shown how to insert data into a Single Lines of Text
3.       Shown how to insert data into a Choice column.
var strSiteUrl = "http://SPSERVER:100";
var objList = new SPAPI_Lists(strSiteUrl);
var strGender="";
function InsertRecord()
{
                if($("#txtTitle").val()=="")
                {
                                alert('Please Select Title');
                                return false;
                }
               
                if($("#ddlCountry").get(0).selectedIndex==0)
                {
                                alert('Please Select Country');
                                return false;
                }
                var strTitle=$("#txtTitle").val();
                var strCountry=$("#ddlCountry").val();
                if($('#rbMale:checked'))
                {
                                strGender="Male";
                }
                if($('#rbFemale:checked'))
                {
                                strGender="Female"
                }
                var oWs = objList.quickAddListItem('JWS Practice', [{Title:(strTitle),Country:(strCountry),Gender:(strGender)}]);
                if(oWs.status==200)
                {
                                alert('Insert Saved Successfully');
                                $("#txtTitle").val("");
                                $("#ddlCountry").get(0).selectedIndex=0;
                                $("#rbMale").attr("checked",true);
                }
}

$(document).ready(function()
{             
                $("#btnSave").click(function()
                {
                                InsertRecord();
                });
               
                BindDropDown();

});

function BindDropDown()
{

    var sQuery = "<Query><OrderBy><FieldRef Name='ID' /></OrderBy></Query>";
    var strViewFields = "<ViewFields><FieldRef Name='Title' /><FieldRef Name='ID' /></ViewFields>";
    var result = objList.getListItems("Country", "", sQuery, strViewFields, "", "", "");
   
    var strBindDropDown = "";
    strBindDropDown += "<Option>";
    strBindDropDown += "--Select Country--";
    var ItemRows = '';
    if ($.browser.webkit)
    {
        ItemRows = "row";
    }
    else
    {
        ItemRows = "z\\:row";
    }
    if ($(result.responseXML).find(ItemRows).length > 0)
    {
        $(result.responseXML).find(ItemRows).each(function ()
        {
            strBindDropDown += "<Option value=" + $(this).attr('ows_ID') + ">";
            strBindDropDown += $(this).attr('ows_Title')
            strBindDropDown += "</Option>";
        });
    }
    strBindDropDown += "</Option>";
    $("#ddlCountry").html(strBindDropDown);
}

Bind HTML Dropdown List using JQuery and SharePoint Web Services



Please go to this post to understand what  JS files you need to download to achieve the required functionality.

In Page:-
You should have the Country dropdown this way.
<select id=’ddlCountry’><option></option></select>


var strSiteUrl = "http://SPSERVER:100";
var objList = new SPAPI_Lists(strSiteUrl);

function BindDropDown()
{

    var sQuery = "<Query><OrderBy><FieldRef Name='ID' /></OrderBy></Query>";
    var strViewFields = "<ViewFields><FieldRef Name='Title' /><FieldRef Name='ID' /></ViewFields>";
    var result = objList.getListItems("Country", "", sQuery, strViewFields, "", "", "");
   
    var strBindDropDown = "";
    strBindDropDown += "<Option>";
    strBindDropDown += "--Select Country--";
    var ItemRows = '';
    if ($.browser.webkit)
    {
        ItemRows = "row";
    }
    else
    {
        ItemRows = "z\\:row";
    }
    if ($(result.responseXML).find(ItemRows).length > 0)
    {
        $(result.responseXML).find(ItemRows).each(function ()
        {
            strBindDropDown += "<Option value=" + $(this).attr('ows_ID') + ">";
            strBindDropDown += $(this).attr('ows_Title')
            strBindDropDown += "</Option>";
        });
    }
    strBindDropDown += "</Option>";
    $("#ddlCountry").html(strBindDropDown);
}
_spBodyOnLoadFunctionNames.push("BindDropDown");