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

Thursday, August 3, 2017

SharePoint WebPart Life Cycle

It's important to know everything about webpart events and the sequence to avoid development mistakes.

Initial Load:

Events fired when a web part is initially loaded.

OnInit 
OnLoad
EnsureChildControls 
CreateChildControls 
EnsureChildControls 
CreateControlCollection 
EnsureChildControls (many) 
OnPreRender 
EnsureChildControls (many) 
SaveViewState 
RenderControl 
Render 
RenderControl 
RenderContents 
RenderChildren 
OnUnload 
Dispose

Post-back:

OnInit 
LoadViewState
EnsureChildControls 
CreateChildControls 
EnsureChildControls 
CreateControlCollection 
EnsureChildControls (many) 
OnLoad
btnUpdate_Click
OnBubbleEvent
EnsureChildControls 
OnPreRender 
EnsureChildControls (many) 
SaveViewState 
RenderControl 
Render 
RenderControl 
RenderContents 
RenderChildren 
OnUnload 
Dispose



Like Asp.Net life cycle there is also Web Part life cycle. So it is better to understand the web part life cycle. 


OnInit: This method handles initialization of the control.

OnLoad: This event handles the Load event. This is also used for initialize the control but is not intended for loading data or other processing functionality.

CreateChildControls: This is the most popular event in web part life cycle. This creates any child controls. So if you are adding any control to display then you have to write in this method.

EnsureChildControls: This method ensures that CreateChildControls has executed. EnsureChildControls method must be called to prevent null reference exceptions.

SaveViewState: View state of the web part saved.

OnPreRender: This method handles or initiates tasks such as data loading that must complete before the control can render. 

Page.PreRenderComplete: The page fires the PreRenderComplete event after all controls have completed their OnPreRender methods.

Render: This method is used to render everything.

RenderContents: Renders the contents of the control only, inside of the outer tags and style properties.

OnUnload: Performs the final cleanup.

Set Default value to Person or Group field in SharePoint 2010 using JavaScript and JQuery

This article is about how to set default value to person or group field when new list form opens using JavsScript and JQuery.

It includes the following

1. Get the members of a SharePoint group using SPServices JQuery library
2. Set the value to Person or Group field using JavaScript

<script type="text/javascript" language="javascript" src="/Style%20Library/JS/jquery-1.8.3.min.js"></script>
<script type="text/javascript" language="javascript" src="/Style%20Library/JS/jquery.SPServices-0.7.2.min.js"></script>
<script type="text/javascript">

    $(document).ready(function() {
      var strHTMLGroupUsers = "";
        $().SPServices({         
        operation: "GetUserCollectionFromGroup",
        groupName: "Approvers",
        async: false,
        completefunc: function(xData, Status) {
        $(xData.responseXML).find("User").each(function() {
          strHTMLGroupUsers += $(this).attr("Name")+";";
        });
      }
        });
        //get the people pickers input div
        var pp = getPickerInputElement("Approvers");
        //set it to the current user if we've found it
        if(pp != null) 
        pp.innerHTML = strHTMLGroupUsers;
        $("img[title='Check Names']").parent().click();

    });

function getPickerInputElement(fieldsInternalName)

    var result  = "";
    var divs = document.getElementsByTagName("DIV");
    for(var i=0; i < divs.length ; i++)
     { 
       if(divs[i].id=="WebPartWPQ3")
        { 
          var tds = divs[i].getElementsByTagName("TD");
          for(var j=0; j < tds.length; j++)
           { 
             var cellHTML = tds[j].innerHTML;
              
             if(cellHTML.indexOf('FieldInternalName="' + fieldsInternalName + '"') >= 0)
             { 
               var innerDivs = tds[j].getElementsByTagName("DIV");
               for(var k=0; k < innerDivs.length; k++)
               { 
                 if(innerDivs[k].id.indexOf("UserField_upLevelDiv") > 0)
                 { 
                   result = innerDivs[k];
                   break;
                 }}}
             }
           }
        }
   return result;
}



</script>
 

Thursday, July 6, 2017

SharePoint 2013 Global Navigation Menu Alignment and Display

Add below CSS to Content Editor WebPart and it will resolve problem.
<style>
.ms-core-listMenu-horizontalBox .menu-item.ms-core-listMenu-horizontalBox  
.menu-item .additional-background.ms-core-listMenu-horizontalBox  
.menu-item .additional-background .menu-item-text { display:block; }
ul.dynamic { min-width:175px; }
span.menu-item-text:hover { 
background-color:rgba(205,230,247,0.5);
padding:2px;
}
span.menu-item-textpadding:2px;
}
</style> 

SharePoint 2013 Search REST API

You can use the Search REST service to submit Keyword Query Language (KQL) or FAST Query Language (FQL) queries in your apps for SharePoint, remote client applications, mobile applications, and other applications.

The Search REST service supports both HTTP POST and HTTP GET requests.

GET requests

Construct the URI for query GET requests to the Search REST service as follows:

/_api/search/query

For GET requests, you specify the query parameters in the URL. You can construct the GET request URL in two ways:
  • http://server/_api/search/query?query_parameter=value&query_parameter=value
  • http://server/_api/search/query(query_parameter=value&query_parameter=)
POST requests

You construct the URI for query POST requests to the Search REST service as follows:

/_api/search/postquery

For POST requests, you pass the query parameters in the request in JavaScript Object Notation (JSON) format.

Disabling ParserEnabled affect: SharePoint 2013

Disabling ParserEnabled affect:
  1. We can't search in document library using Office document properties.
  2. When you upload image file. Thumbnail will not be generated.
  3. When you save a list template, You will not see it in list template gallery.
If you need to disable this property you need to take care of its effect. Disable it only in special sites. if you want it when you upload document using code disable it temporary using code like below:

using (var site = new SPSite("http://sharepoint-portal"))
{
    using (var web = site.OpenWeb())
    {
       SPFile file = web.GetFolder("Documents").Files["mydoc.docx"]; 
       using (var fs = new FileInfo(@"C:\Documents\mydoc.docx").OpenRead())
 
       {
        documentBytes = ..... // get the documents bytes
       }
       web.ParserEnabled = false;
       web.Update();
       file.SaveBinary(documentBytes);
       web.ParserEnabled = true;  
       web.Update();
    }
}