Saturday, September 20, 2014

Client Object Model sample codes

Hi there,

I worked for few items in the client object model using ECMA script and here are the sample codes I am sharing for the example.

The below code gives you the web title and web id.

<script type="text/javascript">
ExecuteOrDelayUntilScriptLoaded(getWebSiteData, "sp.js");
var context = null;
var web = null;
function getWebSiteData() {
context = new SP.ClientContext.get_current();
web = context.get_web();
context.load(web);
context.executeQueryAsync(Function.createDelegate(this, this.onSuccessMethod), Function.createDelegate(this, this.onFailureMethod));
}
function onSuccessMethod(sender, args) {
alert('web title:' + web.get_title() + '\n ID:' + web.get_id());
}
function onFaiureMethodl(sender, args) {
alert('request failed ' + args.get_message() + '\n' + args.get_stackTrace());
} </script>
view raw gistfile1.js hosted with ❤ by GitHub

The below code adds the new list to the site with updating the list title, description, template and then added to the web,



<script type="text/ecmascript">
ExecuteOrDelayUntilScriptLoaded(createList, "sp.js");
function createList() {
var clientContext = new SP.ClientContext.get_current();
var oWebsite = clientContext.get_web();
var listCreationInfo = new SP.ListCreationInformation();
listCreationInfo.set_title('COMList1'); // list name
listCreationInfo.set_description('description'); // list description
listCreationInfo.set_templateType(SP.ListTemplateType.genericList); //list type
oWebsite.get_lists().add(listCreationInfo);
clientContext.executeQueryAsync(
Function.createDelegate(this, this.onQuerySucceeded),// when success
Function.createDelegate(this, this.onQueryFailed) // when failed
);
}
function onQuerySucceeded() {
alert("List Created");
}
function onQueryFailed(sender, args) {
alert("List Failed");
}</script>
view raw gistfile1.js hosted with ❤ by GitHub

No comments: