Friday, April 14, 2017

Get all web applications, site collections and sites in a farm using powershell

You can get all web application and site collection in each web application and sub sites in each site collection in a SharePoint farm using the below powershell code

$webApp = Get-SPWebApplication
write-host $webApp
foreach ($webApps in $webApp)
{
write-host “WEBAPPLICATION :” $webApps.Name
foreach ($site in $webApps.Sites)
{


write-host “SITE: ” $site.URL
foreach($wb in $site.webs)
{
write-host "web: ” $wb.URL
}

}
}

save this as .ps1 extension and run the SharePoint management shell with administrator privilege and open the .ps1 file saved and you will get  list of web applications name and site collection in it and sub sites in that site collection one by one.

Happy Coding !!!

Friday, March 3, 2017

Get all sub sites in a site using rest api in SharePoint

You can get all sub sites in a site using rest api in SharePoint.

Here is the URL to get all sub sites

http://yoursite/_api/Web/webinfos

This will give you the properties of sub site like :-

  • Title
  • Description
  • Web template used for the site (e.g. Team site, project site etc) 
  • Server relative url 
  • Date of creation etc in json format



Happy Coding !!!


Monday, February 20, 2017

Reference error:GetCtxRgiidFromlid is not defined

Recently i encountered a weird issue in SharePoint 2013, when i click on any document library context menu it shows "Reference error:GetCtxRgiidFromlid is not defined" as shown below



After lot of googling and some hit and trial methods, i figured out that this issue is related to the sequence of SharePoint js files in my custom master page.

I used sp.init.js and sp.runtime.js  and gave the reference as
 <script src="/_layouts/15/sp.init.js"></script>
<script src="/_layouts/15/sp.runtime.js"></script>

I gave theses reference because i need to execute some functions (SharePoint jsom api) and these files were required.

But this lead to the reference error so i modified the custom master page and given the links as

<!--MS:<SharePoint:ScriptLink language="javascript" name="sp.runtime.js" OnDemand="true" runat="server" Localizable="false">-->
        <!--ME:</SharePoint:ScriptLink>-->
        <!--MS:<SharePoint:ScriptLink language="javascript" name="sp.init.js" OnDemand="true" runat="server" Localizable="false">-->

First sp.runtme.js is to be loaded then sp.init.js after doing these changes above error was gone.

Happy Coding !!


Thursday, February 16, 2017

Use CK editor in SharePoint default forms

The SharePoint multi column field has some issues as it does not render properly except in internet explorer.

Here i am going to explain you today how you can use third party text editor to use in SharePoint default forms (newform.aspx/editform.aspx). I used ck editor here.

This editor supports multiple editing options and moreover it renders properly in all the browsers.

First i created a columns named "FullHTMLColumn" multiple line of text and choose plain text from
Specify the type of text to allow option 

This columns will store the html content in multiple line of text form

Now you have to place the ck editor files in document library or you can give live links, here i have placed all the editor files in style library and given the reference.
Don't forget to give the jQuery reference, in my case i have given the reference in master page.

Then open the list's newform.aspx and find the PlaceHolderMain and placed the below script in that

<script src="../../Style Library/ckeditor/ckeditor.js"></script>
<script>
var FullHTMLColumn="";


$(document).ready(function(){
FullHTMLColumn=$("[title='FullHTMLColumn']").attr('id');



CKEDITOR.replace(FullHTMLColumn);

});
 function PreSaveAction() { 

$("[title='FullHTMLColumn']").val("<DIV>"+CKEDITOR.instances[FullHTMLColumn].getData()+"</DIV>");


  return true;
  }

</script>

Now open the newform.aspx from browser you will see all the editor options are present on the above column as shown below.

Now you can perform all the text editing option here and save the data in to list.
Use the same script on editform.aspx to show same editor options on that form also



















Now on allitems.aspx and viewform.aspx place the below script under placeholdermain content placeholder.

This script will render the html content inside the multi line of text column, if you will not place this script html will be rendered as plain text

<script type="text/javascript">
$(document).ready(function(){

function render_html()
{

var theTDs = document.getElementsByTagName("TD");

var i=0;

var TDContent = " ";

while (i < theTDs.length) {

try {

TDContent = theTDs[i].innerText || theTDs[i].textContent;

if ((TDContent.indexOf("<DIV") == 0) && (TDContent.indexOf("</DIV>") >= 0)) {

theTDs[i].innerHTML = TDContent;

}

}

catch(err){}

i=i+1;

}

}

render_html();

});

 </script>

Happy Coding !!!

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!!!