Wednesday 18 December 2013

SharePoint Get Specific Folder List Items

You can also check whether item is folder or list item using "FileSystemObjectType"

var context = SP.ClientContext.get_current();
var web = context.get_web();

//doc is a document library server relative url
var folder = web.getFolderByServerRelativeUrl("Documents/Wildlife");

//var parentFolder = folder.get_parentFolder();
//context.load(parentFolder);

context.load(folder);
context. executeQueryAsync(function(){

var query = new SP.CamlQuery();
query.set_folderServerRelativeUrl(folder.get_serverRelativeUrl());
var allItems = list.getItems(query);
context.load(allItems, "Include(Title, FileSystemObjectType, File)");

context.executeQueryAsync(function(){
debugger;
var itemsEnumerator = allItems.getEnumerator();
while(itemsEnumerator.moveNext()){
var item = itemsEnumerator.get_current();
var fileType = item.get_fileSystemObjectType();
//get the detailed information

 var title = item.get_item("Title")

}
}, function(sender, args){debugger;});

}, function(sender, args){debugger;});
Read More

Thursday 12 December 2013

SharePoint Upload File to Document Library using Client Side Object Model

"use strict";

var JJ = window.JJ || {};
JJ.Jsom = JJ.Jsom || {};

JJ.Jsom.Libs = function () {

    var deferreds = new Array(),

    upload = function (library, filename, file) {
        deferreds[deferreds.length] = $.Deferred();
debugger;
        getFileBuffer(file).then(
            function (buffer) {
                var bytes = new Uint8Array(buffer);
                var content = new SP.Base64EncodedByteArray(); //base64 encoding
                for (var b = 0; b < bytes.length; b++) {
                    content.append(bytes[b]);
                }
                var ctx = new SP.ClientContext.get_current();

var oList = ctx.get_web().get_lists().getByTitle(library);

fileCreateInfo = new SP.FileCreationInformation();
fileCreateInfo.set_url(filename);
fileCreateInfo.set_content(content);
fileCreateInfo.set_overwrite(true);
               
this.newFile = oList.get_rootFolder().get_files().add(fileCreateInfo);

ctx.load(this.newFile);
ctx.executeQueryAsync(
Function.createDelegate(this, function(){alert('Done');}),
Function.createDelegate(this, function(){alert(arguments[1].get_message());})
);

            },
            function (err) {
                deferreds[deferreds.length - 1].reject(err);
            }
         );

        return deferreds[deferreds.length - 1].promise();

    },

    getFileBuffer = function (file) {
        var deferred = $.Deferred();
var reader = new FileReader();
reader.onload = function (e) {
deferred.resolve(e.target.result);
}
reader.onerror = function (e) {
deferred.reject(e.target.error);
}
reader.readAsArrayBuffer(file);
return deferred.promise();
    };

    return {
        upload: upload,
    };

}();

$(document).ready(function(){
SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function(){});
$("#s4-bodyContainer").prepend('<input id="inputFile" type="file" /><input id="uploadDocumentButton" type="Button" value="Upload Document"/>');

$("#uploadDocumentButton").unbind('click').click(function () {
debugger;
if (document.getElementById("inputFile").files.length === 0) {
alert("Select a file!");
return;
}

var parts = document.getElementById("inputFile").value.split("\\");
var filename = parts[parts.length - 1];
var file = document.getElementById("inputFile").files[0];

JJ.Jsom.Libs.upload("Documents", filename, file);
});
});

Read More

Monday 9 December 2013

SharePoint Get List AttachMents

SP.SOD.executeFunc('sp.js', 'SP.ClientContext', function(){
debugger;
 var ctx=new SP.ClientContext("http://nit-hv5-idm2:11111/sites/hr/referral");
  var list = ctx.get_web().get_lists().getByTitle("Job Opening");
 var query = new SP.CamlQuery();
     query.set_viewXml('');
 var listFields = list.getItems(query);
 ctx.load(listFields);
ctx.executeQueryAsync(Function.createDelegate(this, function(sender,args){
debugger;var i=0;
   var listEnumerator = listFields.getEnumerator();
  while (listEnumerator.moveNext()) {
if(i++ == 0){
    var oField = listEnumerator.get_current();
    var attachments = oField.get_attachmentFiles();
    ctx.load(attachments);

ctx.executeQueryAsync(Function.createDelegate(this, function(sender, args){
debugger;
var enums = attachments.getEnumerator();
while (enums.moveNext()) {
var attacha = enums.get_current();
console.log(attacha.get_serverRelativeUrl());
}
 
console.log(attachments);
 

}), Function.createDelegate(this, function(){}));  

}
 }
}), Function.createDelegate(this, function(){}));
});

Read More

Translate

Total Pageviews

Powered By Blogger · Designed By Seo Blogger Templates