Tuesday, September 5, 2017

backup restore external content type and external list

1. Create ECT in SPD using Secure Store as Authentication

2. Configure in BCS as required

3. Export the ECT from SPD on Dev

4. Set up identical Secure Store on Test

5. Import BCS Model in Test

6. Set Permissions as required on ECT

7. Create list from ECT

backup restore external content type and external list 

Thursday, August 3, 2017

SharePoint: CAML Query not working based on WHERE clause

When we write the CAML query in SharePoint development and if we put WHERE clause in query it still returns all records.


For example look at the below query, it works fine in U2U builder but returns all records if we use the same in code.


SPQuery query = new SPQuery();
query.Query = "<Query><Where></Where></Query>"


Here is the Fix:

Remove the "<Query>" Tag and rewrite it

SPQuery query = new SPQuery();
query.Query = "<Where></Where>"

SharePoint: SPSecurity.RunWithElevatedPrivileges method

This is used to execute the specified method with Full Control rights even if the logged in user does not otherwise have Full Control.

SPSecurity.RunWithElevatedPrivileges(delegate()
{
    // Your code here...
});


Note:

1.The RunWithElevatedPrivileges method can not be used within a sandbox solution 

2.The item that is actioned with elevated privileges will be executed under the web applications App pool account in IIS, which means that the modified user of the document/list item will most likely display as SYSTEM ACCOUNT.

3.If you did not want to execute the code as the App Pool account for any reason, you could alternatively use a specific user account to execute the code under by implementing the SPUserToken class.

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>