Thursday, 9 March 2017

JavaScript JQuery and REST API Links



http://ramdotnetdeveloper.blogspot.in/2015/09/fill-dropdown-from-list-using-jsom-ecma.html


http://ramdotnetdeveloper.blogspot.in/2015/09/ecmascript-client-object-model-retrieve.html

Add record into SharePoint List using JavaScript

Add record into SharePoint List using JavaScript


HTML Control
  <div>
        <table>
        <tr>
            <td colspan="2" >
                <label style="width: 200px; height: 50px; font-size: large; font: bold;">Customer Details</label>
            </td>
        </tr>
            <tr>
                <td>
                    <label for="txtFirstName">First Name</label>
                </td>
                <td>
                    <input id="txtFirstName" type="text" name="txtFirstName" />
                </td>

            </tr>
            <tr>
                <td>
                    <label for="txtLastName">Last Name</label>
                </td>
                <td>
                    <input id="txtLastName" type="text" name="txtLastName" />
                </td>

            </tr>
            <tr>
                <td>
                    <label for="txtoccupation">Occupation:</label>
                </td>
                <td>
                    <input id="txtoccupation" type="text" name="txtoccupation" />
                </td>

            </tr>
            <tr>
                <td>
                    <label for="txtlocation">Location:</label>
                </td>
                <td>
                    <input id="txtlocation" type="text" name="txtlocation" />
                </td>

            </tr>
            <tr>
                <td>
                    <label for="ddlCountry">Country:</label>
                </td>
                <td>
                    <select id="ddlCountry" name="ddlCountry">
                        <option value="0" selected>Select</option>
                        <option value="India">India</option>
                        <option value="USA">USA</option>
                        <option value="UK">UK</option>
                        <option value="SA">SA</option>
                    </select>
                </td>

            </tr>


            <tr>
                <td>
                    <label for="txtComments">Comments:</label>
                </td>
                <td>
                    <textarea id="txtComments" name="txtComments" cols="40" rows="5"></textarea>
                </td>

            </tr>

            <tr>
                <td>
                    <input id="btnsumbit" type="submit" value="Add" />
                </td>
                <td>
                   <input id="btnCancel" type="submit" value="Cancel" onclick="" />
                </td>
            </tr>

        </table>

    </div>


JS File:-

var clientcontext;
var hostWebUrl;
var appWebUrl;
var AddCustomerID = 0;


$(document).ready(function () {
    hostWebUrl = decodeURIComponent(manageQueryStringParameter('SPHostUrl'));
    appWebUrl = decodeURIComponent(manageQueryStringParameter('SPAppWebUrl'));
    //Add method
    $("#btnsumbit").click(function () {
        AddItemsToList();
    });
   
});


//This function is used to get the hostweb url
function manageQueryStringParameter(paramToRetrieve) {
    var params =
    document.URL.split("?")[1].split("&");
    var strParams = "";
    for (var i = 0; i < params.length; i = i + 1) {
        var singleParam = params[i].split("=");
        if (singleParam[0] == paramToRetrieve) {
            return singleParam[1];
        }
    }
}


//Add List Item to SP host web
function AddItemsToList() {
    var ctx = new SP.ClientContext(appWebUrl);//Get the SharePoint Context object based upon the URL
    var appCtxSite = new SP.AppContextSite(ctx, hostWebUrl);
    var web = appCtxSite.get_web(); //Get the Site  
    var list = web.get_lists().getByTitle("Customer"); //Get the List based upon the Title
    var listCreationInformation = new SP.ListItemCreationInformation(); //Object for creating Item in the List
    var listItem = list.addItem(listCreationInformation);
    listItem.set_item("Title", $("#txtFirstName").val());
    listItem.set_item("FirstName", $("#txtFirstName").val());
    listItem.set_item("LastName", $("#txtLastName").val());
    listItem.set_item("Occupation", $("#txtoccupation").val());
    listItem.set_item("Location", $("#txtlocation").val());
    listItem.set_item("Country", $("#ddlCountry").val());
    //var selectedText = $("#ddlCountry").find("option:selected").text(); Mango
    //var selectedValue = $("#ddlCountry").val();  1
    listItem.set_item("Comments", $("#txtComments").val());
    listItem.update(); //Update the List Item
    //AddCustomerID = listItem.get_id();
    ctx.load(listItem);
    //Execute the batch Asynchronously
    ctx.executeQueryAsync(
    Function.createDelegate(this, success),
    Function.createDelegate(this, fail)
    );
}

function success() {
    //alert("Item added successfully");
    //alert('Item added successfully: ' + AddCustomerID);
    $("#divSubmitMessage").html("<div style= 'color:green'>Record has been submitted</div>");
    ClearFields();
}

function fail(sender, args) {
    //alert('Failed to get user name. Error:' + args.get_message());
    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}


function ClearFields() {
    $('#txtFirstName').val('');
    $('#txtLastName').val('');
    $('#txtoccupation').val('');
    $('#txtlocation').val('');
    $('#ddlCountry').prop('selectedIndex', 0);  
    $('#txtComments').val('');
}

Clear the Control using Jquery

Clear the Control using Jquery

   <div>
        <table>
        <tr>
            <td colspan="2" >
                <label style="width: 200px; height: 50px; font-size: large; font: bold;">Customer Details</label>
            </td>
        </tr>
            <tr>
                <td>
                    <label for="txtFirstName">First Name</label>
                </td>
                <td>
                    <input id="txtFirstName" type="text" name="txtFirstName" />
                </td>

            </tr>
            <tr>
                <td>
                    <label for="txtLastName">Last Name</label>
                </td>
                <td>
                    <input id="txtLastName" type="text" name="txtLastName" />
                </td>

            </tr>
            <tr>
                <td>
                    <label for="txtoccupation">Occupation:</label>
                </td>
                <td>
                    <input id="txtoccupation" type="text" name="txtoccupation" />
                </td>

            </tr>
            <tr>
                <td>
                    <label for="txtlocation">Location:</label>
                </td>
                <td>
                    <input id="txtlocation" type="text" name="txtlocation" />
                </td>

            </tr>
            <tr>
                <td>
                    <label for="ddlCountry">Country:</label>
                </td>
                <td>
                    <select id="ddlCountry" name="ddlCountry">
                        <option value="0" selected>Select</option>
                        <option value="India">India</option>
                        <option value="USA">USA</option>
                        <option value="UK">UK</option>
                        <option value="SA">SA</option>
                    </select>
                </td>

            </tr>


            <tr>
                <td>
                    <label for="txtComments">Comments:</label>
                </td>
                <td>
                    <textarea id="txtComments" name="txtComments" cols="40" rows="5"></textarea>
                </td>

            </tr>

            <tr>
                <td>
                    <input id="btnsumbit" type="submit" value="Add" />
                </td>
                <td>
                   <input id="btnCancel" type="submit" value="Cancel" onclick="" />
                </td>
            </tr>

        </table>
    </div>




Clear the Control using Jquery


function ClearFields() {
    $('#txtFirstName').val('');
    $('#txtLastName').val('');
    $('#txtoccupation').val('');
    $('#txtlocation').val('');
    $('#ddlCountry').prop('selectedIndex', 0);  
    $('#txtComments').val('');
}















Wednesday, 15 February 2017

Paging in the SharePoint list

Paging in the SharePoint 2010 list is done using the



The SPListItemCollectionPosition class supports paging through data sets, storing the state that is needed to get the next page of data for a specific view of a list. 


ListItemCollectionPosition

Gets or sets an object that is used to obtain the next set of rows in a paged view of a list. The below code I have refined from the following link. It will return the given page like page 3, items say 4.


public static SPListItemCollection ExecuteCAMLToRetrieveListItemsInPages( string listName, string viewName, 
       string caml, string[] columnNames, int pageIndex, int pageItemCount) 
   {       
       SPListItemCollection postDetailsItems = null;         
       using (SPSite Site = new SPSite(SPContext.Current.Site.Url))         
       {             
           using (SPWeb sharePointWeb = Site.AllWebs[XYZ])             
           {                
               // Check if the SharePoint web object is not null or not.                 
               if (sharePointWeb == null)                 
               {                 
               }                 
               // Get the Post List                 
               SPList postList = sharePointWeb.Lists[listName];                 
               // Get the SPLIst View                 
               SPView listView = postList.Views[viewName];                 
               SPQuery query = new SPQuery(listView);                 
               query.Query = caml;                 
               // Retrieve the items for the last page. E.g.: If request is for 5th page and item count/page=10 then row limit will retrieve
               //40 items and 40th item will be used to get the column details which will be used for pagination.                 
               query.RowLimit = (uint)(pageItemCount * (pageIndex - 1));                 
               postDetailsItems = postList.GetItems(query);                 
               // Get the previous page last item position. Use this item to retrieve the column details which will be used for pagination.                 
               int previousPageLastItemPosition = postDetailsItems.Count - 1;                 
               StringBuilder columnBuilder = new StringBuilder();                 
               // Form the paging filter query string                 
               if (columnNames != null)                 
               {                     
                   foreach (string column in columnNames)                     
                   {                        
                       // Make sure that if the field value is mandatory and if you are passing it as NULL then SPList.GetItems will throw exception.
                       string columnValue = 
                           (postDetailsItems[previousPageLastItemPosition][column] == null) ? string.Empty : postDetailsItems[previousPageLastItemPosition][column].ToString();
                       // Check if the value is null or empty                         
                       columnBuilder.Append("&p_" + column + "=" + columnValue);                     
                   }                
               }                 
               query = new SPQuery(listView);                 
               query.Query = caml;                 
               // Create Paging Information which will be used for retrieving paging based items                 
               SPListItemCollectionPosition objSPListColPos = new SPListItemCollectionPosition("Paged=TRUE" + columnBuilder.ToString());                 
               query.RowLimit = uint.Parse(pageItemCount.ToString());                 
               query.ListItemCollectionPosition = objSPListColPos;                 
               // Execute the CAML query.                 
               postDetailsItems = postList.GetItems(query);             
           }         
       }         return postDetailsItems;     
   }

Monday, 30 January 2017

Convert from Window to claim based web application through command in MOSS2010


Convert from Window to claim based web application through command in MOSS2010

$setcba = Get-SPWebApplication "http://ibggn6756-2:1111/"
$setcba.UseClaimsAuthentication = 1;
$setcba.Update()

Wednesday, 25 January 2017

Update password in sharepoint Farm and WebSites

SharePoint Installation:-

Update password in sharepoint Farm and WebSites
1. runat --> SharePoint Timer --> right click properties change password.


stsadm -o updatefarmcredentials -userlogin IBTS\ved.p1 -password feb@345345

Operation status completely.
.

Thursday, 29 September 2016

Add Print Option to SharePoint Page to print one particular web Part using Content Editor Web Part

Add Print Option to SharePoint Page to print one particular web Part using Content Editor Web Part


Add Print Option to SharePoint Page to print one particular web Part using Content Editor Web Part

When the javascript code point to a particular Web Part by its web part ID, then the print option will try to print only the chosen web part.

<input type="button" ID="printBtn1" OnClick="javascript:void(PrintWebPart())" value="Print only tranSMART WebPart">

<script language="JavaScript">

//Controls which Web Part or zone to print
var WebPartElementID = "WebPartWPQ2";

//Function to print Web Part
function PrintWebPart()
{
var bolWebPartFound = false;
if (document.getElementById != null)
{
//Create html to print in new window
var PrintingHTML = '<HTML>\n<HEAD>\n';
//Take data from Head Tag
if (document.getElementsByTagName != null)
{
var HeadData= document.getElementsByTagName("HEAD");
if (HeadData.length > 0)
PrintingHTML += HeadData[0].innerHTML;
}
PrintingHTML += '\n</HEAD>\n<BODY>\n';
var WebPartData = document.getElementById(WebPartElementID);
if (WebPartData != null)
{
PrintingHTML += WebPartData.innerHTML;
bolWebPartFound = true;
}
else
{
bolWebPartFound = false;
alert ('Cannot Find Web Part');
}
}
PrintingHTML += '\n</BODY>\n</HTML>';
//Open new window to print
if (bolWebPartFound)
{
var PrintingWindow = window.open("","PrintWebPart", "toolbar,width=800,height=600,scrollbars,resizable,menubar");
PrintingWindow.document.open();
PrintingWindow.document.write(PrintingHTML);

// Open Print Window
PrintingWindow.window.print();
PrintingWindow.document.close();

}
}
</script>

We can find the web part ID by viewing the source of a page.

Leave your comments below.








Add Print Option to SharePoint Page using Content Editor Web Part

Add Print Option to SharePoint Page

Print option can be added to a content Editor Web Part. This will enable Printing of all content within a SharePoint Page without the top part and left navigation.

<script type="text/javascript" src="http://sample.com/sites/test/JSFiles/jquery-1.4.2.min.js"></script>

<div>
<input id="printBtn" type="button" Value="Print all WebParts" alt="Print this page" style="margin-top:10px;margin-left:10px;"/>
</div>

<script>

$(document).ready(function()
{
var strlink="";
$("link[rel='stylesheet']").each(function()
{
strlink+="<link rel = 'stylesheet' href='"+$(this).attr('href')+"' type='text/css'/>";
});

$("#printBtn").click(function()
{

var htmlStr ="<html><head>"+strlink+"</head><body>"+$("#MSO_ContentTable").parent().html()+"</body></html>";

var PrintingWindow = window.open("about:blank","","toolbar,width=800,height=600,scrollbars,resizable,menubar");

PrintingWindow.document.open();
PrintingWindow.document.write(htmlStr);
PrintingWindow.document.close();
PrintingWindow.focus();
PrintingWindow.document.getElementById("printBtn").style.display="none";
PrintingWindow.print();

});

});
</script>

Leave your comments below.