Skip to main content

SharePoint 2010 ECMA Scripts code snippets


ECMScript 

ECMScript you need to create a client context before you can retrieve data from a SharePoint site.

With ECMAScript you can retrieve the client context from the SharePoint site in which the script runs.
Use the Load method to specify which data to load from SharePoint.

Use the asynchronous method executeQueryAsync to retrieve the information from SharePoint. This method needs a reference to a callback method when the execution succeeds and a reference to a callback method when the execution fails.

To Set and Get the Properties there are ways to get the information.
  1. get_methodname
  2. set_Methodname
example get_web(), set_title etc




Update, Insert, Delete Code snippets


1. Load Site Data  


<script type="text/javascript">
var clientContext = null;
var web = null;
ExecuteOrDelayUntilScriptLoaded(Initialize, "sp.js");
function Initialize()
{
clientContext = new SP.ClientContext.get_current();
web = clientContext.get_web();
clientContext.load(web, 'Title');
clientContext.executeQueryAsync(Function.createDelegate(this, this.onSiteLoadSuccess), Function.createDelegate(this, this.onQueryFailed));
}
function onSiteLoadSuccess(sender, args) {
alert("site title : " + web.get_title());
}

function onQueryFailed(sender, args) {
alert('request failed ' + args.get_message() + '\n' + args.get_stackTrace());
}</script>​

2.Load List Items Data 

<script type="text/javascript">
var clientContext = null;
var web = null;
ExecuteOrDelayUntilScriptLoaded(Initialize, "sp.js");
function Initialize()
{
clientContext = new SP.ClientContext.get_current();
web = clientContext.get_web();
var list = web.get_lists().getByTitle("Pages");
var camlQuery = new SP.CamlQuery();
var q = '<View><RowLimit>5</RowLimit></View>';
camlQuery.set_viewXml(q);
this.listItems = list.getItems(camlQuery);
clientContext.load(listItems, 'Include(DisplayName,Id)');
clientContext.executeQueryAsync(Function.createDelegate(this, this.onListItemsLoadSuccess),
Function.createDelegate(this, this.onQueryFailed));
}
function onListItemsLoadSuccess(sender, args) {
var listEnumerator = this.listItems.getEnumerator();
//iterate though all of the items
while (listEnumerator.moveNext()) {
   var item = listEnumerator.get_current();              
   var title = item.get_displayName();
   var id = item.get_id();
            alert("List title : " + title + "; List ID : "+ id);
        }
}

function onQueryFailed(sender, args) {
alert('request failed ' + args.get_message() + '\n' + args.get_stackTrace());
}</script>​

3. Load List Data


<script type="text/javascript">
var clientContext = null;
var web = null;
ExecuteOrDelayUntilScriptLoaded(Initialize, "sp.js");
function Initialize()
{
clientContext = new SP.ClientContext.get_current();
web = clientContext.get_web();
this.list = web.get_lists().getByTitle("Images");
clientContext.load(list, 'Title', 'Id');
clientContext.executeQueryAsync(Function.createDelegate(this, this.onListLoadSuccess),
Function.createDelegate(this, this.onQueryFailed));
}
function onListLoadSuccess(sender, args) {
alert("List title : " + this.list.get_title() + "; List ID : "+ this.list.get_id());
}

function onQueryFailed(sender, args) {
alert('request failed ' + args.get_message() + '\n' + args.get_stackTrace());
}</script>​

4.  Create List


<script type="text/javascript">

var clientContext = null;
var web = null;

ExecuteOrDelayUntilScriptLoaded(Initialize, "sp.js");

function Initialize()
{

clientContext = new SP.ClientContext.get_current();

web = clientContext.get_web();

var itemCreateInfo = new SP.ListItemCreationInformation();

var listCreationInfo = new SP.ListCreationInformation();

listCreationInfo.set_title('My Custom Generic List');

listCreationInfo.set_templateType(SP.ListTemplateType.genericList);

this.oList = web.get_lists().add(listCreationInfo);

clientContext.load(oList, 'Title', 'Id');

clientContext.executeQueryAsync(Function.createDelegate(this, this.onListCreateSuccess),

Function.createDelegate(this, this.onQueryFailed));

}

function onListCreateSuccess(sender, args) {
alert("List title : " + this.oList.get_title() + "; List ID : "+ this.oList.get_id());
}

function onQueryFailed(sender, args) {
alert('request failed ' + args.get_message() + '\n' + args.get_stackTrace());
}

</script>​

5. Create Site


<script type="text/javascript">
var clientContext = null;
var web = null;
ExecuteOrDelayUntilScriptLoaded(Initialize, "sp.js");
function Initialize()
{
clientContext = new SP.ClientContext.get_current();
web = clientContext.get_web();
var webCreateInfo = new SP.WebCreationInformation();
    webCreateInfo.set_description("All about Rare solutions.");
    webCreateInfo.set_language(1033);
    webCreateInfo.set_title("Rare Solutions - SharePoint and .NET");
    webCreateInfo.set_url("RareSolutionsSharePoint");
    webCreateInfo.set_useSamePermissionsAsParentSite(true);
    webCreateInfo.set_webTemplate("BLOG#0");

    this.oNewWebsite = this.web.get_webs().add(webCreateInfo);

    clientContext.load(this.oNewWebsite, 'ServerRelativeUrl', 'Created');

clientContext.executeQueryAsync(Function.createDelegate(this, this.onCreateWebSuccess),

Function.createDelegate(this, this.onQueryFailed));
}
function onCreateWebSuccess(sender, args) {
alert("Web site url : " + this.oNewWebsite.get_serverRelativeUrl());
}

function onQueryFailed(sender, args) {
alert('request failed ' + args.get_message() + '\n' + args.get_stackTrace());
}</script>​

6. Update List



<script type="text/javascript">
var clientContext = null;
var web = null;
ExecuteOrDelayUntilScriptLoaded(Initialize, "sp.js");
function Initialize()
{
clientContext = new SP.ClientContext.get_current();
web = clientContext.get_web();
this.list = web.get_lists().getByTitle('My custom generic list');
list.set_description('My custom generic list description');
list.update();
clientContext.load(list, 'Description');
clientContext.executeQueryAsync(Function.createDelegate(this, this.onSiteLoadSuccess), Function.createDelegate(this, this.onQueryFailed));
}
function onSiteLoadSuccess(sender, args) {
alert("list description : " + this.list.get_description());
}

function onQueryFailed(sender, args) {
alert('request failed ' + args.get_message() + '\n' + args.get_stackTrace());
}</script>​

7.  Update List Item


<script type="text/javascript">
var clientContext = null;
var web = null;
ExecuteOrDelayUntilScriptLoaded(Initialize, "sp.js");
function Initialize()
{
clientContext = new SP.ClientContext.get_current();
web = clientContext.get_web();
this.list = web.get_lists().getByTitle('My custom generic list');
this.oListItem = list.getItemById(1);
oListItem.set_item('Title', 'Praveen Battula Updated');
oListItem.update();

clientContext.executeQueryAsync(Function.createDelegate(this, this.onUpdateListItemSuccess), Function.createDelegate(this, this.onQueryFailed));
}
function onUpdateListItemSuccess(sender, args) {
alert("list item updated");
}

function onQueryFailed(sender, args) {
alert('request failed ' + args.get_message() + '\n' + args.get_stackTrace());
}</script>


8. Delete List



<script type="text/javascript">
var clientContext = null;
var web = null;
ExecuteOrDelayUntilScriptLoaded(Initialize, "sp.js");
function Initialize()
{
clientContext = new SP.ClientContext.get_current();
web = clientContext.get_web();
this.list = web.get_lists().getByTitle('My custom generic list');
list.deleteObject();

clientContext.executeQueryAsync(Function.createDelegate(this, this.onListDeleteSuccess),

Function.createDelegate(this, this.onQueryFailed));
}
function onListDeleteSuccess(sender, args) {
alert("list deleted");
}

function onQueryFailed(sender, args) {
alert('request failed ' + args.get_message() + '\n' + args.get_stackTrace());
}</script>​

9. Delete Site



<script type="text/javascript">
var clientContext = null;
var web = null;
ExecuteOrDelayUntilScriptLoaded(Initialize, "sp.js");
function Initialize()
{
clientContext = new SP.ClientContext.get_current();
web = clientContext.get_web();
this.website = web.get_webs().getByTitle('Rare Solutions');
website.deleteObject();

clientContext.executeQueryAsync(Function.createDelegate(this, this.onSiteDeleteSuccess),

Function.createDelegate(this, this.onQueryFailed));
}
function onSiteDeleteSuccess(sender, args) {
alert("site deleted");
}

function onQueryFailed(sender, args) {
alert('request failed ' + args.get_message() + '\n' + args.get_stackTrace());
}</script>​

10. Delete List Item



<script type="text/javascript">
var clientContext = null;
var web = null;
ExecuteOrDelayUntilScriptLoaded(Initialize, "sp.js");
function Initialize()
{
clientContext = new SP.ClientContext.get_current();
web = clientContext.get_web();
this.list = web.get_lists().getByTitle('My custom generic list');
this.oListItem = list.getItemById(1);
oListItem.deleteObject();

clientContext.executeQueryAsync(Function.createDelegate(this, this.onListItemDeleteSuccess),

Function.createDelegate(this, this.onQueryFailed));
}
function onListItemDeleteSuccess(sender, args) {
alert("list item deleted");
}

function onQueryFailed(sender, args) {
alert('request failed ' + args.get_message() + '\n' + args.get_stackTrace());
}</script>​

Comments

Popular posts from this blog

SharePoint RPC Protocols Examples Using OWSSVR.DLL

What is SharePoint RPC Protocols? Part 1 This reference includes information about the methods and usage of SharePoint Foundation Remote Procedure Call (RPC) protocol. This protocol can be used in Win32-based applications or in ASPX applications to make HTTP POST requests to the server. Methods in this protocol that do not modify the contents of the database can also be used in URL protocol to make HTTP GET requests. Definition taken from http://msdn.microsoft.com/en-us/library/ms448359.aspx You will find the OWSSVR.DLL in SharePoint 2010 Server Physical Path: C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI and MOSS C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\ISAPI OWSSVR.DLL List of commands DialogView  Display ExportList GetProjSchema GetUsageBlob HitCounter RenderView To read more about the OWSSVR.DLL command Please read the URL Protocol from Microsoft Blog having a URL http://msdn.microsoft.com/en...

SharePoint 2013 Search Database Part 1

SharePoint 2013 Search architecture drastically change, as earlier in FS4SP 2010 we have 2 search applications “FASTContentSSA” and “FASTQuerySSA” and 7 total database in use. Following are the List of DB (FS4SP) FASTContentSSA Search Service Application DB Search Service Application Crawl Store DB Search Service Application Property Store DB FASTQuerySSA Search Service Application DB Search Service Application Crawl Store DB Search Service Application Property Store DB FASTSearchAdminDatabase : Fast Search Admin Database  In SharePoint 2013 search has only 1 Search Service application and 4 database in use. No property store database need any more, now the properties are directly stored inside the index component and all the index directly indexed to the physical system where FS4SP data comes from database as well as from the File system now data directly stored and indexed/ retried from the Physical disk because of this performance increase and search experien...

Send Email using SharePoint Rest API

We have a requirement to send email vai SharePoint Rest API. We dig and find the below solution to send email using SharePoint API. Point Must be remember End point " /_api/SP.Utilities.Utility.SendEmail " end point to send email.  In JSON __metadata we used the type " SP.Utilities.EmailProperties ". The User / Group must have a read permission for particular site. where we placed this site code. JS Dependency  JQuery Code URL:  https://gist.github.com/BasantPandey/05189832546f2c6cc0bd008fcfec3264#file-sharepointsendemail-js Code  var Email = function (){ // Email Either email groupname or email address var from = ' abc.yahoo.com ' , to = ' abc.yahoo.com ' , cc = ' abc.yahoo.com ' , subject = ' My Email Subject ' ; this . options = this . options || {}; this . options [ ' fromEmail ' ] = this . options [ ' fromEmail ' ] || {}; this . options [ ' toEmail ...