Tuesday, February 24, 2015



In last article we saw how to create a new folder in document library using SharePoint REST Services. Now we will see how to delete the folder using SharePoint REST services.

Following is the SharePoint REST service URL:

/_api/web/GetFileByServerRelativeUrl('/[LIBRARY_NAME]/[FOLDER_NAME]')

Parameter: server relative URL of folder.
HTTP Method: POST
and remember header should contain following lines:

                       "X-RequestDigest": digest,
                       "IF-MATCH": "*",
                       "X-HTTP-Method" :"DELETE"

Following is the sample code:

<html>
<head>
<script src="/Site%20Assets/js/jquery-1.11.1.min.js"></script>
<script>

function DeleteFolder()
{
var digest = "";

$.ajax(
{
                url: "/_api/contextinfo",
                method: "POST",
                headers: {
                                "ACCEPT": "application/json;odata=verbose",
                                "content-type": "application/json;odata=verbose"
                },
                success: function (data) {
                digest = data.d.GetContextWebInformation.FormDigestValue;
                },
                error: function (data) {                
                }
}).done(function() {

                $.ajax(
                                {                                            
                                                url: "/_api/web/GetFileByServerRelativeUrl('/testdoc/test1')",
                                                method: "POST",                                                                                          
                                                headers: {
                                                                "ACCEPT": "application/json;odata=verbose",                                                                                                                                  
                                                                "X-RequestDigest": digest,
                                                                "IF-MATCH": "*",
                                                                "X-HTTP-Method" :"DELETE"
                                                },                                                                                                                          
                                                success: function (data) {                                          
                                                                alert("Delete successfully");                    
                                                },
                                                error: function (data) {
                                                                alert("Error occured." + data.responseText);
                                                }
                                });                        
                });
                       
}

</script>
</head>
<body>
<div>
                <input type="submit" onclick="DeleteFolder()" value="Delete Folder"> </input>
</div>
</body>
</html>



Happy SharePoint coding!

Sunday, February 22, 2015


Till now we learned how to fetch folder information using SharePoint REST services. Now we will see how to create a new folder using SharePoint REST service.

Following is the SharePoint REST service:

"/_api/web/folders"

All you need to pass to this service is the Server Relative URL. You can create this using the folder's Server Relative URL in which you want to create folder plus new folder's name.

Following is the sample code:

<html>
<head>
<script src="/Site%20Assets/js/jquery-1.11.1.min.js"></script>
<script>

function AddFolder()
{
var digest = "";
var folderName = $("#txtFName").val();
$.ajax(
{
                url: "/_api/contextinfo",                                                                              
                method: "POST",
                headers: {
                                "ACCEPT": "application/json;odata=verbose",
                                "content-type": "application/json;odata=verbose"
                },
                success: function (data) {
                digest = data.d.GetContextWebInformation.FormDigestValue;
                },
                error: function (data) {
                                //alert("Error occured." + data);
                }
}).done(function() {
                $.ajax(
                {
                url: "/_api/web/folders",
                data: JSON.stringify({ '__metadata': { 'type': 'SP.Folder' }, 'ServerRelativeUrl': '/testdoc/' + folderName}),
                method: "POST",
                headers: {
                                "ACCEPT": "application/json;odata=verbose",
                                "content-type": "application/json;odata=verbose",
                                "X-RequestDigest": digest
                },
                success: function (data) {
                                alert("Done successfully");
                },
                error: function (data) {
                                alert("Error occured." + data);
                }
});          
});                                          
}
</script> 
</head>
<body>
<div>
                <input type="text"  value="" id="txtFName"> </input>
</div> 
<div>
                <input type="submit" onclick="AddFolder()" value="Add Folder"> </input>
</div>
</body>
</html>


Happy SharePoint coding.


In one of my article we saw how to fetch files from SharePoint Root folders using SharePoint REST services. Now In this article we will see how to fetch the files from a particular folder using SharePoint REST services.

Following is the SharePoint REST API:

"/_api/web/GetFolderByServerRelativeUrl('/LIBRARY_NAME/FOLDER_NAME')/folders"

Following is the sample code:

<html>
<head>
<script src="/Site%20Assets/js/jquery-1.11.1.min.js"></script>
<script>

function GetAllFoldersofRootFolders()
{
                $.ajax(
                {
                url: "/_api/web/GetFolderByServerRelativeUrl('/testdoc/testfolder')/folders",     
                method: "GET",
                headers: {
                                "ACCEPT": "application/json;odata=verbose",
                                "content-type": "application/json;odata=verbose"                         
                },
                success: function (data) {
                                var htmlStr = "";
                                $.each(data.d.results, function(){
                               
                                                if(htmlStr === "")
                                                {
                                                                htmlStr = "<li 'ServerRelativeUrl="+ this.ServerRelativeUrl + "'>"  + this.Name  + “</li>";
                                                }
                                                else
                                                {
                                                                htmlStr = htmlStr + "<li 'ServerRelativeUrl="+ this.ServerRelativeUrl + "'>"  + this.Name +  “</li>";
                                                }
                               
                                });
                               
                                $("#foldersDiv").html(htmlStr);
                },
                error: function (data) {
                                alert("Error occured." + data);
                }
});         
                                               

</script>

</head>
<body>
<div>
                <input type="submit" onclick="GetAllFoldersofRootFolders()" value="Get All Files of folder"> </input>
</div>

<div>
                <ul id="foldersDiv">
                </ul>
</div>
</body>
</html>

Happy SharePoint coding.


In one of my article we saw how to get SharePoint folders of root folder of document library using SharePoint REST Services. In this article we will see how to get folders within folders. This scenario is helpful to build up folder structure of library.

Following is the complete SharePoint REST URL :

"/_api/web/GetFolderByServerRelativeUrl('/testdoc/folder1')/folders"

All you need to pass here is the path of folder. Then SharePoint REST service returns the list of folders within that folder.

Complete sample code:

<html>
<head>
<script src="/Site%20Assets/js/jquery-1.11.1.min.js"></script>
<script>

function GetAllFoldersofRootFolders()
{
                $.ajax(
                {
                url: "/_api/web/GetFolderByServerRelativeUrl('/testdoc/folder1')/folders",     
                method: "GET",
                headers: {
                                "ACCEPT": "application/json;odata=verbose",
                                "content-type": "application/json;odata=verbose"                         
                },
                success: function (data) {
                                var htmlStr = "";
                                $.each(data.d.results, function(){
                               
                                                if(htmlStr === "")
                                                {
                                                                htmlStr = "<li 'ServerRelativeUrl="+ this.ServerRelativeUrl + "'>"  + this.Name + "(" + this.ItemCount + ")</li>";
                                                }
                                                else
                                                {
                                                                htmlStr = htmlStr + "<li 'ServerRelativeUrl="+ this.ServerRelativeUrl + "'>"  + this.Name + "(" + this.ItemCount + ")</li>";
                                                }
                               
                                });
                               
                                $("#foldersDiv").html(htmlStr);
                },
                error: function (data) {
                                alert("Error occured." + data);
                }
});                                                     

</script> 
</head>
<body>
<div>
                <input type="submit" onclick="GetAllFoldersofRootFolders()" value="Get All Folders of folder"> </input>
</div>

<div>
                <ul id="foldersDiv">
                </ul>
               
</div>
</body>
</html>

Happy SharePoint coding.

Saturday, February 21, 2015


In last article we saw how to get top level folders. Now lets see how to get all SharePoint library files from the root folder or top level folders using SharePoint REST API. Using REST services you can get files of library. in this article we will see how to get files at top level or from Root folder. We will see how to fetch files within folders in coming article.

Following is the URL to get files :

"/_api/web/lists/getbytitle('testdoc')/RootFolder/files"

The only difference with folder URL is that it replaces 'folders' by 'files'.

Following is the sample code:

<html>
<head>
<script src="/Site%20Assets/js/jquery-1.11.1.min.js"></script>
<script> 
function GetAllFoldersofRootFolders()
{
                $.ajax(
                {
                url: "/_api/web/lists/getbytitle('test1')/RootFolder/files",     
                method: "GET",
                headers: {
                                "ACCEPT": "application/json;odata=verbose",
                                "content-type": "application/json;odata=verbose"                         
                },
                success: function (data) {
                                var htmlStr = "";
                                $.each(data.d.results, function(){
                               
                                                if(htmlStr === "")
                                                {
                                                                htmlStr = "<li 'ServerRelativeUrl="+ this.ServerRelativeUrl + "'>"  + this.Name + “</li>";
                                                }
                                                else
                                                {
                                                                htmlStr = htmlStr + "<li 'ServerRelativeUrl="+ this.ServerRelativeUrl + "'>"  + this.Name + “</li>";
                                                }
                               
                                });
                               
                                $("#foldersDiv").html(htmlStr);
                },
                error: function (data) {
                                alert("Error occured." + data);
                }
});          
                                               
}

</script>

</head>
<body>
<div>
                <input type="submit" onclick="GetAllFoldersofRootFolders()" value="Get All Folders of Root folder"> </input>
</div>

<div>
                <ul id="foldersDiv">
                </ul>
               
</div>
</body>
</html>


Happy SharePoint coding. 

SharePoint Libraries are the basic feature of SharePoint from its first release. Over the year there is so much change in SharePoint programming but the basic entities like lists and libraries remains unchanged. In SharePoint 2013 REST services are new way of programming. So lets see how to work with folders and files using SharePoint REST services. There will  be several articles I will publish regarding files and folders. In this article we will see how to get top folders of library.

Following is the REST URL you need to use :

"/_api/web/lists/getbytitle(LIBRARY_NAME')/RootFolder/folders"


In above REST you need to take care of certain things like appending 'folders' at the end of URL. The html method is 'GET' .

Complete Sample Code:

<html>
<head>
<script src="/Site%20Assets/js/jquery.min.js"></script>
<script> 
function GetAllFoldersofRootFolders()
{
                $.ajax(
                {
                url: "/_api/web/lists/getbytitle('test1')/RootFolder/folders",               
                method: "GET",
                headers: {
                                "ACCEPT": "application/json;odata=verbose",
                                "content-type": "application/json;odata=verbose"                         
                },
                success: function (data) {
                                var htmlStr = "";
                                $.each(data.d.results, function(){
                               
                                                if(htmlStr === "")
                                                {
                                                                htmlStr = "<li 'ServerRelativeUrl="+ this.ServerRelativeUrl + "'>"  + this.Name + "(" + this.ItemCount + ")</li>";
                                                }
                                                else
                                                {
                                                                htmlStr = htmlStr + "<li 'ServerRelativeUrl="+ this.ServerRelativeUrl + "'>"  + this.Name + "(" + this.ItemCount + ")</li>";
                                                }
                               
                                });
                               
                                $("#foldersDiv").html(htmlStr);
                },
                error: function (data) {
                                alert("Error occured." + data);
                }
});          
                                               
}

</script>

</head>
<body>
<div>
                <input type="submit" onclick="GetAllFoldersofRootFolders()" value="Get All Folders of Root folder"> </input>
</div>

<div>
                <ul id="foldersDiv">
                </ul>
               
</div>
</body>
</html>

To execute above code provide the correct name of existing library. This code returns folders of Root folder of SharePoint Library.

Happy SharePoint coding.

Wednesday, February 4, 2015


Many of my friends have asked me about how to get groups to which user has joined. We don't have such RESTAPI URL available in Yammer documentation. But sometimes this is very important to get users all groups. Then how can you get this information? So my friend here we go!

Though API is not there but you can use other REST API URL using which you can get the groups of a user. And that API URL is nothing but Autocomplete '. The main use of this API url is to give suggestions to user when types group name or user name. It returns few records on the basis of match found. This API has its own parameters but we will see them in detail in another article. In this article we will focus on to get the user's groups.

Then main parameters which are required here are: Prefix and models.

URL: autocomplete/ranked .

Prefix: In our case we will provide the email address or short name of user as prefix.

Models: We will provide 'user' and count '1'. Which means match for user type and return one record for provided email address in prefix.

Another REST url you needed is to get the details of group on the basis of id returned by above REST url. Details of second url:

URL: /groups/[Group Id].json 

Parameter: you need to pass the group id in above url.

By this input you will get the detail of that particular user. And in that detail you can get the group list as shown in below pic.



Following is the complete script which returns groups of a user. You need to provide data app id and user email address in order to execute this code.

Complete Code:


<script type="text/javascript" data-app-id="[YourAppId]" src="https://c64.assets-yammer.com/assets/platform_js_sdk.js"></script> 
<script type="text/javascript">
yam.getLoginStatus(
function (response) {
if (response.authResponse) {
}
else {
yam.platform.login(function (response) {
if (!response.authResponse) {
}
});
}
});

function GetMsg(){
var prefixValue = "[USER_SHORT_NAME]";
yam.platform.request({
url: "autocomplete/ranked",
method: "GET",
accept: "application/json",
data: { prefix: prefixValue, models: 'user:1,group:0'},
success: function (responseData) {
for(var i=0; i< responseData.user[0].group_ids.length; i++)
{
yam.platform.request(
 { url: "groups/" + responseData.user[0].group_ids[i] + ".json"
 , method: "GET"
 , success: function (msg) {  
var node = document.createElement("LI");
var textnode = document.createTextNode(msg.full_name);
node.appendChild(textnode);
document.getElementById("myGroups").appendChild(node);  
 }
 , error: function (msg) { 
 
 }
 }
);
}
},
error: function (msg) {
alert("Error..." + msg);
}
}
)
}

</script> 
<input type="button" onclick='GetMsg()' value="Click Me"></input> 
<div id="groupsId">
<ul id="myGroups">
</ul>

</div>

Output Screen:




Happy Yammer coding!