Showing posts with label CSOM. Show all posts
Showing posts with label CSOM. Show all posts

Tuesday, May 28, 2013

Set and Get property bag values in SharePoint 2013 apps using CSOM

Property bags are a place to store metadata or properties of SharePoint 2013 site. A Property bag is a key value pair, that can be used in Farm, Web Application, Site and Web level.
Following example shows how to write and read the property bag values of host web.
Following code shows how to write value in property bag. Here I am writing value for “Directory Tagged” key.

function tag() {
    var hostweburl = decodeURIComponent(getQueryStringParameter("SPHostUrl"));
    var appweburl = decodeURIComponent(getQueryStringParameter("SPAppWebUrl"));
    var clientContext = new SP.ClientContext.get_current();
    var hostContext = new SP.AppContextSite(clientContext, hostweburl);
    var web = hostContext.get_web();
    clientContext.load(web);
    var webProperties = web.get_allProperties();
    clientContext.load(webProperties);
    webProperties.set_item("DirectoryTagged", true);
    web.update();
    clientContext.executeQueryAsync(TaggedSuccess, TaggedFail);
}

function TaggedSuccess(sender, args) {
    $("#results").text("Tagged your site.");
}

function TaggedFail(sender, args) {
    $("#results").text("Tagging failed");
}

In the following code Iam reading the value of “Directory Tagged” key value and the value is ‘True’ Iam disabling the button

function checkIfTagged() {
    var hostweburl = decodeURIComponent(getQueryStringParameter("SPHostUrl"));
    var appweburl = decodeURIComponent(getQueryStringParameter("SPAppWebUrl"));
    var clientContext = new SP.ClientContext.get_current();
    var hostContext = new SP.AppContextSite(clientContext, hostweburl);   
    var web = hostContext.get_web();
    clientContext.load(web);

    //get all web properties
    webProperties = web.get_allProperties();
    clientContext.load(webProperties);   
    clientContext.executeQueryAsync(getWebPropertiesSucceeded, getWebPropertiesFailed);
}

function getWebPropertiesSucceeded() {

    //Get all property fields
    var allProps = webProperties.get_fieldValues();
    var customProp = "";

    //Check property exists or not
    if (webProperties.get_fieldValues().DirectoryTagged != undefined) {
        var customProp = webProperties.get_fieldValues().DirectoryTagged;
    }   

    if (customProp == "TRUE")
    {
        //property value is true                       
        $("#results").text("Site tag is true.");
        $("#btnTag").attr("disabled", "disabled");
        return;
    }
    //property value is false
    $("#btnTag").bind("click", tag);
    $("#messageText").text("Site is tag is false.");
}

function getWebPropertiesFailed(args, sender) {
//log error message

Monday, May 27, 2013

Add current logged-in user to a new list item by using SharePoint 2013 Client Object Model

While working in SharePoint event registration with SharePoint-hosted app by using Client Side Object Model. In that code I had a requirement to add the current logged in user to list item. Here is the code to get the current logged-in user and add a new list item to the new list item.

var currentContext;
var web;
var user;
$(document).ready(init());

function init() {
//get current context
currentContext = SP.ClientContext.get_current();

//get current web details
web = currentContext.get_web();

getCurrentUser();
addNewItem();
}

function getCurrentUser() {
user = web.get_currentUser();
context.load(user);
context.executeQueryAsync(onGetUserNameSuccess, onGetUserNameFailed);
}

function onGetUserNameSuccess() {
//custom code with current logged in user
}

function onGetUserNameFailed(sender, args) {
//log the error
}

function addNewItem()
{
// get the list for your item
var list = context.get_web().get_lists().getByTitle("ListName");

// create a ListItemCreationInformation object
var itemCreateInfo = new SP.ListItemCreationInformation();

// add this item to the list
var newItem = list.addItem(itemCreateInfo);

// Create a UserValue and set the LookupId to the current user
var userValue = new SP.FieldUserValue();
userValue.LookupId = user.get_id();

// Create a LookupValue and set the lookupId to the id of an event
var eventValue = new SP.FieldLookupValue();
eventValue.set_lookupId($("#Lookup Coloumn").val()); 

newItem.set_item('Title', user); //internal names
newItem.set_item('EventLookup', eventValue);
newItem.update();

context.load(newItem);
context.executeQueryAsync(
Function.createDelegate(this, this.onAddNewItemSucceeded),
Function.createDelegate(this, this.onAddNewItemFailed)
);
}

function onAddNewItemSucceeded() {
//custom code when Item Added Successfully
}
function onAddNewItemFailed(sender, args) {
//log the error

}