Saturday, 4 March 2017

Rest API SharePoint 2013

Basic ODATA Queries on SharePoint 2013 List


OutPut :





    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js" type="text/javascript"></script>
    <script src="/_layouts/15/sp.runtime.js" type="text/javascript"></script>
    <script src="/_layouts/15/sp.js" type="text/javascript"></script>

    <script type="text/javascript">
        $(document).ready(function(){
            jQuery("#webtitleid").click(GetWebTitle);
            jQuery("#CurrentUser").click(GetCurrentUser);
            jQuery("#createList").click(CreateList);
            jQuery("#listitems").click(GetListItems);
            jQuery("#addlistitem").click(AddListItem);
      
        });

        function AddListItem() {

            var call = jQuery.ajax({
                url: _spPageContextInfo.webAbsoluteUrl + "/_api/Web/Lists/getByTitle('Products')/Items",
                type: "POST",
                datatype: "json",
                headers:
                    {
                        Accept: "application/json;odata=verbose",
                        "Content-Type": "application/json;odata=verbose",
                        "X-RequestDigest": jQuery("#__REQUESTDIGEST").val()
                    },
                data:
                    JSON.stringify({
                        "__metadata": { type: "SP.Data.ProductsListItem" },                  
                         Title: "ItemAddedthroughMyRest"
                    })

            });
            call.done(function (data, textstatus, jqXHR) {
                var msg = jQuery("#additem");
                msg.text("ListItem Added Successfully");
            });

            call.fail(function (data, textstatus, jqXHR) {
                alert('failed to Add ListItem');

            });
        }

        function GetListItems()
        {
            var call = jQuery.ajax({
                url: _spPageContextInfo.webAbsoluteUrl + "/_api/Web/Lists/getByTitle('Products')/Items?$select=Title",
                type: "GET",
                datatype: "json",
                headers:
                    {
                        Accept: "application/json;odata=verbose"

                    }
            });
            call.done(function (data, textstatus, jqXHR) {
                var msg = jQuery("#litems");

                jQuery.each(data.d.results, function (index, value) {
                    msg.append("<li>" +value.Title +"</li>");
                });
            });

            call.fail(function (data, textstatus, jqXHR) {
                alert("failed to get list items");
            });
        }
        function CreateList()
        {
            var call = jQuery.ajax({
                url: _spPageContextInfo.webAbsoluteUrl + "/_api/Web/Lists",
                type: "POST",
                datatype: "json",
                headers:
                    {
                        Accept: "application/json;odata=verbose",
                        "Content-Type": "application/json;odata=verbose",
                        "X-RequestDigest": jQuery("#__REQUESTDIGEST").val()
                    },
                data:
                    JSON.stringify({
                            "__metadata": { type: "SP.List" },
                            BaseTemplate: SP.ListTemplateType.genericList,
                             Title: "MyRestAPICustomList"
                                 })

            });
            call.done(function (data, textstatus, jqXHR) {
                var msg = jQuery("#list");
                msg.text("List Created Successfully");
            });

            call.fail(function (data, textstatus, jqXHR) {
                alert('failed to Create List');

            });
        }

        function GetWebTitle()
        {
            var call = jQuery.ajax({
                url: _spPageContextInfo.webAbsoluteUrl + "/_api/Web/",
                type: "GET",
                datatype:"json",
                headers:
                {
                    Accept: "application/json;odata=verbose"
                }
            });

            call.done(function (data, textstatus, jqXHR) {
                var msg = jQuery("#webtitle");
                  msg.text("Web Title is :").append(data.d.Title);
            });
            call.fail(function (data, textstatus, jqXHR) {
                alert('failed to get the result');
            });

        }
            function GetCurrentUser()
        {
            var call = jQuery.ajax({
                url:_spPageContextInfo.webAbsoluteUrl + "/_api/Web/?$select=CurrentUser&$expand=CurrentUser",
                type:"GET",
                datatype: "json",
                headers:
                {
                    Accept : "application/json;odata=verbose"

                }
            });
            call.done(function (data, textstatus, jqXHR) {
                var msg = jQuery("#User");
                     msg.text("User Title is :").append(data.d.CurrentUser.Title);

            });
            call.fail(function (data, textstatus, jqXHR) {
                alert('failed to get the result');

            });

        }
    </script>

    <div>
        <p id="webtitle"></p>
        <p id="User"> </p>
        <p id="list"> </p>
        <ul id="litems"></ul>
        <p id="additem"></p>
        <input id="webtitleid" type="button" value="GetWebTitle" />
        <input id="CurrentUser" type="button" value="GEtCurrentUser" style="margin: 10px;" />
        <input id="createList" type="button" value="CreateList" style="margin: 10px;" />
        <input id="listitems" type="button" value="GetProductsListItems" style="margin: 10px;" />
        <input id="addlistitem" type="button" value="AddProductsListitem" style="margin: 10px;" />

        <br />

    </div>

No comments:

Post a Comment