Showing posts with label SharePoint 2013. Show all posts
Showing posts with label SharePoint 2013. Show all posts

Thursday, February 2, 2017

Create List Items using rest Api in SharePoint

You can create lists records using the powerful rest API that SharePoint provide.

Here is an example how you can do it



<script src="https://code.jquery.com/jquery-3.1.0.min.js" type="text/javascript"></script>
<script type="text/javascript">
function AddListItem() {
         var category = $("select[id$='ddlcategory'] option:selected").text();
        var feedback = $("[id$='txtfeedback']").val();
        $.ajax
            ({
                url: _spPageContextInfo.webAbsoluteUrl + "/_api/Web/Lists/GetByTitle('Feedback')/Items",
                type: "POST",
                data: JSON.stringify
                ({
                    __metadata:
                    {
                        type: "SP.Data.FeedbackListItem"
                    },
                    Title: category,
                    Feedback: feedback
                   
                }),
                headers:
                {
                    "Accept": "application/json;odata=verbose",
                    "Content-Type": "application/json;odata=verbose",
                    "X-RequestDigest": $("#__REQUESTDIGEST").val(),
                    "X-HTTP-Method": "POST"
                },
                success: function (data, status, xhr) {

                },
                error: function (xhr, status, error) {

                }
            });
    
    }
</script>
I created this function to create list records in my Feedback list.
I am getting a string value from a text box and from one select box and passing these value to list fields and called this function on button click.

You have to take care of the below code

  data: JSON.stringify
                ({
                    __metadata:
                    {
                        type: "SP.Data.FeedbackListItem"    // This is the list title and the syntax to create item
                    },
                    Title: category,  //These are internal names of fields to which you will be passing values
                    Feedback: feedback
                   
                }),

Happy Coding !!!

Wednesday, January 18, 2017

Get List items using Rest Api and Angular js in SharePoint

Hello Folks,

I am going to explain you how to get list items from SharePoint list using restful api and angular js.

First of all you need to get the angular js file, you can get online link or you can download it from here https://angularjs.org/ (version 1) and place inside any SharePoint library

Place a script editor web part on page and add below script.
<script type="text/javascript" src="/Style Library/vmware/js/angular.min.js"></script>

<script type="text/javascript">
    var myAngApp = angular.module('div_ngapp', [ ]);
    myAngApp.controller('div_ngcontroller', function ($scope, $http) {
        $http({
            method: 'GET',
            cache: true,
            url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getByTitle('Test')/items?$select=Title,ID",
            headers: { "Accept": "application/json;odata=verbose" }
        }).success(function (data, status, headers, config) {
            $scope.listitems= data.d.results;

        }).error(function (data, status, headers, config) {

        });
      
    });
<div  ng-app="div_ngapp">
             <div ng-controller="div_ngcontroller">
                       <div class="row" ng-repeat="item in listitems">
                                <h3>{{item.Title}}{{item.ID}}</h3>
                                      
                        </div>
              </div>
</div>

Let me explain you whole script

   var myAngApp = angular.module('div_ngapp', [ ]);

This is needed to activate angular js and it should be one of the html tag. In my case i have created a div and give its ng-app value as div_ngapp. You can try with different names as you wish.

Now you have to initialize controller function as 

 myAngApp.controller('div_ngcontroller', function ($scope, $http) {
        $http({
            method: 'GET',
            cache: true,
            url: _spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getByTitle('Test')/items?$select=Title,ID",
            headers: { "Accept": "application/json;odata=verbose" }
        }).success(function (data, status, headers, config) {
            $scope.listitems= data.d.results;

        }).error(function (data, status, headers, config) {

        });
      
    }); 


You have to create another div inside your ng-app div and give its ng-controller value in my case i have created a div div_ngcontroller and passed its value in above function

Specify method as 'GET' 

Pass the url _spPageContextInfo.webAbsoluteUrl this will give you the web absolute url and you have to give the list title from where you want to get the items

spPageContextInfo.webAbsoluteUrl + "/_api/web/lists/getByTitle('Test')/items?$select=Title,ID"

Above will get the items from list 'Test' and get only the fields specified after $select  as you can see i am getting only title and id fields. You have to give the internal name of the fields after select.

success(function (data, status, headers, config) {
            $scope.listitems= data.d.results;

In success function getting the list items in a scope variable; in my case i have created a scope array named listitems and all result is stored here.

In html you have to create the following structure

<div  ng-app="div_ngapp">
             <div ng-controller="div_ngcontroller">
                       <div class="row" ng-repeat="item in listitems">
                                <h3>{{item.Title}}{{item.ID}}</h3>
                                      
                        </div>
              </div>
</div>

ng-app and ng-controller i have already explained.

 ng-repeat="item in listitems" 

This causes the html structure below this tag to repeat like  a for loop.

listitems is scope array variable and item is another variable. You can give any names as you wish to give. Now you will get values by item variable

 <h3>{{item.Title}}{{item.ID}}</h3> will be repeated n-times where n is number of items in list ( $scope.listitems)

To get the value you have to pass inside curly braces {{item.Title}} this gets the item title value inside h3 tag

Similarly you can get any column value by using this syntax.

Happy Coding!!!









Wednesday, July 16, 2014

Something went wrong SharePoint App issue

Problem: 

Whenever SharePoint hosted or Auto hosted app code or html  is updated and you upload it to app catalog, and add the newly updated app into site you get the Something Went wrong error message.

Resolution

1) When you remove the app from the web, it will go into the recycle bin of that web and the site collection of that web. You need to remove it from the web recycle bin and site collection's recycle bin. In site collection recycle bin choose "Deleted from end user recycle bin" and then delete the app

2) Second method is to create a new GUID for the app and then open Appmanifest.xml with code and then replace the Product ID with the newly created GUID. In this way app will be registered with a different product id.

3) Third is change the version of app and then upload it again to app catalog site.

Happy Programming !!!

Thursday, May 29, 2014

Get Choice Field values using Client Side Object Model in SharePoint 2013

Folks,

In this post i will explain you how to retrieve choice field values in Client Side Object Model (CSOM) in SharePoint 2013. I have tested this code in Auto hosted app on SharePoint online environment. You can use this code snippet to get values from choice field for custom filter and search conditions.

 var spContext = SharePointContextProvider.Current.GetSharePointContext(Context);

            using (var clientContext = spContext.CreateUserClientContextForSPHost())
            {
                List queslist = clientContext.Web.Lists.GetByTitle("YourListTitle");
                Field choice = queslist.Fields.GetByInternalNameOrTitle("FieldName");

                FieldChoice fldChoice = clientContext.CastTo<FieldChoice>(choice);
                clientContext.Load(fldChoice, f => f.Choices);
                clientContext.ExecuteQuery();
                foreach (string  item in fldChoice.Choices)
                {
                    ddltopic.Items.Add(item.ToString());
//add choices to dropdown list
                }
}

You have to get the SharePoint context, then get the list field and then get that field choice and use the choice fields as per your needs, i have added the choice values in a drop-down list.

Let me know if you face any problem..

Happy Coding :)