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, November 29, 2012

Real Time Logic Retrieving next 15 Days Due Date Tasks from Tasks List



DateTime str = (DateTime)DateTime.Today.AddDays(15);
            string strNewDate = str.Year + "-" + str.Month + "-" + str.Date.Day;
            SPWeb currentWeb = SPContext.Current.Web;
            SPList lst = currentWeb.Lists["Tasks"];
            SPQuery sQuery = new SPQuery();
            sQuery.Query = "<Where><Geq><FieldRef Name='DueDate' /><Value Type='DateTime'>"+strNewDate+"</Value></Geq></Where><OrderBy><FieldRef Name='DueDate' /></OrderBy>";
            SPListItemCollection myColl = lst.GetItems(sQuery);
            if (myColl.Count > 0)
            {
                dgvTasks.DataSource = myColl.GetDataTable();
                dgvTasks.DataBind();
            }

Checking the Current Logged User Group using ECMA Script

var strCheckLoggedUser="";
var strCurrentUserName="";

function CheckAdminGroup()
{
    var currentContext = new SP.ClientContext();
    strCurrentUserName= currentContext.get_web().get_currentUser();
    currentContext.load(strCurrentUserName);
   
    var groupCollection = currentContext.get_web().get_siteGroups();
    var _group = groupCollection.getById(3);
    //Note:- Here 3 is the Admin Group ID.
   
    strCheckLoggedUser= _group.get_users();
    currentContext.load(strCheckLoggedUser);
    currentContext.executeQueryAsync(Function.createDelegate(this, this.UserAvailableinAdminGroup), Function.createDelegate(this, this.UserNotAvailableinAdminGroup));
}

function UserAvailableinAdminGroup()
{
    debugger;
    var strValidUser=false;
    var listEnumerator = strCheckLoggedUser.getEnumerator();
    while (listEnumerator.moveNext())
    {
        var item = listEnumerator.get_current();
        if (strCurrentUserName.get_loginName() == item.get_loginName())
        {
            strValidUser=true;
            break;
        }
    }
   
    if(strValidUser==true)
    {
        //Write your Functionality what u want to perform if the user logged in is a Admin User
        alert('Logged in user is Admin User');
    }
}

function UserNotAvailableinAdminGroup()
{
   
}

ExecuteOrDelayUntilScriptLoaded(CheckAdminGroup, "sp.js");

Getting the User Name and Login Name of the currently Logged User using ECMA Script

var context = ""; 
var web = ""; 
var currentUser = ""; 
    function getLoggedUser() { 
        context = new SP.ClientContext.get_current(); 
        web = context.get_web(); 
        currentUser = web.get_currentUser(); 
         currentUser.retrieve(); 
        context.load(web); 
        context.executeQueryAsync(Function.createDelegate(this, this.onSuccessMethod), Function.createDelegate(this, this.onFailureMethod)); 
    } 
    function onSuccessMethod(sender, args) { 
        var strCurrentLoggedUser = web.get_currentUser(); 
        alert('User name:' + strCurrentLoggedUser .get_title() + '\n Login Name:' + strCurrentLoggedUser .get_loginName()); 
    } 
    function onFailureMethod(sender, args) { 
        alert('request failed ' + args.get_message() + '\n' + args.get_stackTrace()); 
    }
   
    ExecuteOrDelayUntilScriptLoaded(getLoggedUser, "sp.js"); 

Search LookUp Column and select the value in the Dropdownlist using ECMA Script and JQuery

var myColl="";
var myColl1="";
var strCurrentWeb="";
var strCurrentSiteUrl="";
function SearchLookUpData()
{
    try
    {
        var context = new SP.ClientContext.get_current();
        var web = context.get_web();
        strCurrentWeb=context.get_url();
        strCurrentSiteUrl=window.location.protocol + '//' + window.location.host + strCurrentWeb;
        var list = web.get_lists().getByTitle('Book Names');
        var sQuery="<View><Query><Where><Eq><FieldRef Name='ID' /><Value Type='Counter'>4</Value></Eq></Where></Query></View>";
        var camlQuery = new SP.CamlQuery();
        camlQuery.set_viewXml(sQuery);
        this.myColl1 = list.getItems(camlQuery);
        context.load(this.myColl1, 'Include(Title, ID, Category)');
        context.executeQueryAsync(Function.createDelegate(this, this.SearchLookUpDataSuccess), Function.createDelegate(this, this.SearchLookUpDataFailed));
    }
    catch(Ex)
    {
        alert(Ex);
    }
}

function SearchLookUpDataSuccess()
{
    var strBookNames="";
    strBookNames+="<Table>";
    if(myColl1.get_count()!=0)
    {
        var listEnumerator = this.myColl1.getEnumerator();
        while (listEnumerator.moveNext())
        {
            var item = listEnumerator.get_current();
            var strCategoryID=item.get_item('Category').get_lookupId();
            $('select#ddlCategory').val(strCategoryID);
            break;
        }
    }
    else
    {
       

    }   
}

function SearchLookUpDataFailed(sender, args)
{
    alert('failed. Message:' + args.get_message());
}

function BindData()
{
    ExecuteOrDelayUntilScriptLoaded(BindLookUpData, "sp.js");
    ExecuteOrDelayUntilScriptLoaded(SearchLookUpData, "sp.js");
}


function BindLookUpData()
{
    try
    {
        //debugger;
        var context = new SP.ClientContext.get_current();
        var web = context.get_web();
        strCurrentWeb=context.get_url();
        strCurrentSiteUrl=window.location.protocol + '//' + window.location.host + strCurrentWeb;
        var list = web.get_lists().getByTitle('Book Category');
        var sQuery="<View><Query><OrderBy><FieldRef Name='ID' /></OrderBy></Query></View>";
        var camlQuery = new SP.CamlQuery();
        camlQuery.set_viewXml(sQuery);
        this.myColl = list.getItems(camlQuery);
        context.load(this.myColl, 'Include(Title, ID)');
        context.executeQueryAsync(Function.createDelegate(this, this.BindLookUpDataSuccess), Function.createDelegate(this, this.BindLookUpDataFailed));
    }
    catch(Ex)
    {
        alert(Ex);
    }
}

function BindLookUpDataSuccess()
{
    var strBookNames="";
    if(myColl.get_count()!=0)
    {
        var listEnumerator = this.myColl.getEnumerator();
        strBookNames+="<Select id='ddlCategory' width='158px'>";
        strBookNames+="<Option>Select Category</Option>";
        while (listEnumerator.moveNext())
        {
            var item = listEnumerator.get_current();
            var strItemID=item.get_item('ID');
            var strTitle=item.get_item('Title');
            strBookNames+="<Option Value='"+strItemID+"'>"+strTitle+"</Option>";
        }
        strBookNames+="</Select>";
    }
    document.getElementById("tdCategory").innerHTML=strBookNames;
}

function BindLookUpDataFailed(sender, args)
{
    alert('failed. Message:' + args.get_message());
}

_spBodyOnLoadFunctionNames.push("BindData");

How to select a particular value or text with in a Dropdownlist using JQuery


HTML Select Value Example:-
$('select#ddlCategory').val("3");
Note:- Here I am selecting the option which contains 3.
HTML Select Text Example:-
$("select#ddlCategory").find("option:contains('SharePoint 2010')").prop("selected", "selected");
Note:- Here I am selecting the option which contains SharePoint 2010

Wednesday, November 28, 2012

Insertion into a LookUp Column using ECMA Script

var myColl="";
var strCurrentWeb="";
var strCurrentSiteUrl="";
function BindLookUpData()
{
    try
    {
        var context = new SP.ClientContext.get_current();
        var web = context.get_web();
        strCurrentWeb=context.get_url();
        strCurrentSiteUrl=window.location.protocol + '//' + window.location.host + strCurrentWeb;
        var list = web.get_lists().getByTitle('Book Category');
        var sQuery="<View><Query><OrderBy><FieldRef Name='ID' /></OrderBy></Query></View>";
        var camlQuery = new SP.CamlQuery();
        camlQuery.set_viewXml(sQuery);
        this.myColl = list.getItems(camlQuery);
        context.load(this.myColl, 'Include(Title, ID)');
        context.executeQueryAsync(Function.createDelegate(this, this.BindLookUpDataSuccess), Function.createDelegate(this, this.BindLookUpDataFailed));
    }
    catch(Ex)
    {
        alert(Ex);
    }
}

function BindLookUpDataSuccess()
{
    var strBookNames="";
    if(myColl.get_count()!=0)
    {
        var listEnumerator = this.myColl.getEnumerator();
        strBookNames+="<Select id='ddlCategory' width='158px'>";
        strBookNames+="<Option>Select Category</Option>";
        while (listEnumerator.moveNext())
        {
            var item = listEnumerator.get_current();
            var strItemID=item.get_item('ID');
            var strTitle=item.get_item('Title');
            strBookNames+="<Option Value='"+strItemID+"'>"+strTitle+"</Option>";
        }
        strBookNames+="</Select>";
    }
    document.getElementById("tdCategory").innerHTML=strBookNames;  
}

function BindLookUpDataFailed(sender, args)
{
    alert('failed. Message:' + args.get_message());
}

function BindData()
{
    ExecuteOrDelayUntilScriptLoaded(BindLookUpData, "sp.js");
}

function SaveLookUpColumn()
{
        debugger;
        var context = new SP.ClientContext.get_current();
        var web = context.get_web();
        strCurrentWeb=context.get_url();
        strCurrentSiteUrl=window.location.protocol + '//' + window.location.host + strCurrentWeb;
        var list = web.get_lists().getByTitle('Book Names');
        var listItemCreationInfo = new SP.ListItemCreationInformation();
        var newItem = list.addItem(listItemCreationInfo);
        newItem.set_item('Title', document.getElementById("txtBookName").value);
        var _newLookupField = new SP.FieldLookupValue();
        _newLookupField.set_lookupId($("#ddlCategory").val());
        newItem.set_item('Category', _newLookupField);
        newItem.update();
        context.executeQueryAsync(Function.createDelegate(this, this.LookUpInsertSuccess), Function.createDelegate(this, this.LookUpInsertFailed));
}

function LookUpInsertSuccess()
{
    alert('LookUp Column Successfuly Inserted');
    document.getElementById("txtBookName").value="";
    $("select#ddlCategory").get(0).selectedIndex=0;
}

function LookUpInsertFailed(sender, args)
{
    alert('failed. Message:' + args.get_message());
}

function InsertLookUpColumn()
{
    ExecuteOrDelayUntilScriptLoaded(SaveLookUpColumn, "sp.js");
}

_spBodyOnLoadFunctionNames.push("BindData");

Binding Top Five Announcements to a simple HTML Table using ECMA Script

var myColl="";
var strCurrentWeb="";
var strCurrentSiteUrl="";
function BindAnnouncementsData()
{
                try
                {
                                var context = new SP.ClientContext.get_current();
                var web = context.get_web();
                strCurrentWeb=context.get_url();
                strCurrentSiteUrl=window.location.protocol + '//' + window.location.host + strCurrentWeb;
                                var list = web.get_lists().getByTitle('Announcements');
                                var sQuery="<View><Query><OrderBy><FieldRef Name='ID' Ascending='False' /></OrderBy><Where><Geq><FieldRef Name='Expires' /><Value Type='DateTime'><Today /></Value></Geq></Where></Query><RowLimit>5</RowLimit></View>";
                var camlQuery = new SP.CamlQuery();
                camlQuery.set_viewXml(sQuery);
                this.myColl = list.getItems(camlQuery);
                context.load(this.myColl, 'Include(Title, ID)');
                                context.executeQueryAsync(Function.createDelegate(this, this.BindAnouncementsDataSuccess), Function.createDelegate(this, this.BindAnnouncementsDataFailed));
                }
                catch(Ex)
                {
                                alert(Ex);
                }
}

function BindAnouncementsDataSuccess()
{
                var strBookNames="";
                strBookNames+="<Table>";
                if(myColl.get_count()!=0)
                {
                                var listEnumerator = this.myColl.getEnumerator();
                                strBookNames+="<Tr>";
                                strBookNames+="<Td Align='Center' class='HeadingClass'>";
                                strBookNames+="Top Five Announcements";
                                strBookNames+="</Td>";
                                strBookNames+="</Tr>";
                                strBookNames+="<Tr>";
                                strBookNames+="<Td class='HeadingClass'>";
                                strBookNames+="Title";
                                strBookNames+="</Td>";
                                strBookNames+="</Tr>";
                                while (listEnumerator.moveNext())
                                {
                                                var item = listEnumerator.get_current();
                                                var strItemID=item.get_item('ID');
                                                var strTitle=item.get_item('Title');
                                                var strUrl=strCurrentSiteUrl+"Lists/Announcements/DispForm.aspx?ID="+strItemID+"&Source="+window.location.href;
                                                strBookNames+="<Tr>";
                                                strBookNames+="<Td class='LoopRecordsClass'>";
                                                strBookNames+="<A href='"+strUrl+"' class='LoopRecordsClass'>"+strTitle+"</a>";
                                                strBookNames+="</Td>";
                                                strBookNames+="</Tr>";

                                }
                }
                else
                {
                                strBookNames+="<Tr>";
                                strBookNames+="<Td  Align='Center' class='HeadingClass'>";
                                strBookNames+="No Announcements Available";
                                strBookNames+="</Td>";
                                strBookNames+="</Tr>";

                }
               
               
                strBookNames+="</Table>";
                document.getElementById("tdAnnouncements").innerHTML=strBookNames;
}

function BindAnnouncementsDataFailed(sender, args)
{
                alert('failed. Message:' + args.get_message());
}

function BindData()
{
                ExecuteOrDelayUntilScriptLoaded(BindAnnouncementsData, "sp.js");
}

_spBodyOnLoadFunctionNames.push("BindData");