Popular Posts

CSOMJavascript

Get the attachments of an Item:

//store the item attachments in a collection
var attachmentColl=item.get_attachmentFiles();// item is an sharepoint list item

//get the first attachment url, url contains the file name with extension
var attachmentsPath=attachmentColl.get_item(0).get_serverRelativeUrl();


Read the file from SharePoint:
var clientContext = new SP.ClientContext.get_current();
var listAttachmentPath="Path of the file"; //ex: /sites/StyleLibrary/one.txt
    var oWebsite = clientContext.get_web();
    clientContext.load(oWebsite);
    clientContext.executeQueryAsync(function () {
        fileUrl = listAttachmentPath;
        $.ajax({
            url: fileUrl,
            type: "GET"
        })
            .done(Function.createDelegate(this, successAttachmentHandler))
            .error(Function.createDelegate(this, errorAttachmentHandler));
    }, errorHandler);
 
}
function successAttachmentHandler(data)
{
//data contains the file content
    var content = data;
}


Get a string between two strings

String.prototype.between = function (prefix, suffix) {
    s = this;
    var i = s.indexOf(prefix);
    if (i >= 0) {
        s = s.substring(i + prefix.length);
    }
    else {
        return '';
    }
    if (suffix) {
        i = s.indexOf(suffix);
        if (i >= 0) {
            s = s.substring(0, i);
        }
        else {
            return '';
        }
    }
    return s;
}


Usage:

var testString="Hello World !!";
var middleString=testString.between('Hello','!!');
//result is World




 Get List Item by ID:
  var web=spcontext.get_web();
  var list=web.get_lists().getByTitle("Name of List");
  var item=list.get_itemById('itemId');
  spcontext.load(item,'column1','column2',...);
  spcontext.executeQueryAsync(successFn,failureFn);


Get list items by CAML query

var list=spcontext.get_web().get_lists().getByTitle("ListName");
var camlQuery=new SP.CamlQuery();
var query="<View><Query><Where><Eq>sample caml query here..</</</";
camlQuery.set_viewXml(query);
var itemColl=list.getItems(camlQuery);
spcontext.load(itemColl);
spcontext.executeQueryAsync(successFn,failureFn);

function successFn()
{
 var itemCount=itemColl.get_count();
 for(i=0;i<itemCount;i++)
 {
  var listItem=itemColl.get_item(i);
  var coumn1Value=listItem.get_item('column1');
  //for lookup columns
  var lookupCoumn1=listItem.get_item('lookupColumn1').get_lookupValue();
 }
}



Update file to doc lib

var list=spcontext.get_web().get_lists().getByTitle("ListName");
var filecreationInfo=new SP.FileCreationInformation();
var fileName="SampleFileName.txt";
filecreationInfo.set_url(fileName);
filecreationInfo.set_content(new SP.Base64EncodedByteArray());
var fileContent="sample content to be added to the file";
for(var i=0;i<fileContent.length;i++)
{
filecreationInfo.get_content().append(fileContent.charCodeAt(i));
}

var newFile=list.get_rootFolder().get_files().add(filecreationInfo);
spcontext.load(newFile);
spcontext.executeQueryAsync(successFn,failureFn);

Update Item by ID

var list=spcontext.get_web().get_lists().getByTitle('ListName');
var item=list.get_itemById('itemID');
item.set_item('Title','Test Title');
item.set_item('column1','Sample data');
item.update();
spcontext.load(item);
spcontext.executeQueryAsync(successFn,failureFn);


Add item to list

var list=spcontext.get_web().get_lists().getByTitle('listTitle');
var itemcreationInfo=new SP.ListItemCreationInformation();
var item=list.addItem(itemcreationInfo);
item.set_item('column1','sample data');
item.set_item('column2','sample data');
item.update();
spcontext.load(item);
spcontext.executeQueryAsync(successFn,failureFn);