Friday, May 25, 2012

How to create host header Web Application in SharePoint 2010

When you create a web application in SharePoint 2010 you specify IIS web site name and a port number to which the web application will be running on, and to browse that web application you normally enter the server IP address and port number in the http url (e.g http://ServerIPAddress:portanumber) or you enter the machine name and port number. If you want to avoid entering IP Address or server machine name in the url and wanting to browse the web application by a domain host name e.g mywebapp.testdomain.com, there is an option called Host Address, while creating the SharePoint web application  you can specify the fully qualified host address which you will use for browsing the web application.

Below steps shows how you can specify the host address and configure it to route the request to your SharePoint web application.

Step1: First you need to decide the host name, let’s say your domain name is testdomain.com, and you want to create a web application with host address mywebapp.testdomain.com

Step2: Now you need to create a DNS entry on your AD server. Open the DNS from Start->Administrative Tools as shown in below screen shot

Step 3:  Explore your domain name testdomain.com and right click to create New Host (A or AAAA) entry.




 


































Step 4:  Specify the new host name and IP address of your SharePoint WFE where your web application will be deployed



  
Step 5:  Once the DNS entry is done, you need to make Registry entry on your SharePoint WFE server. To make registry entry on the WFE server, just type regedit in Start->Run window

From the Registry Editor, explore HKEY_LOCAL_MACHIN -> System -> CurrentControlSet -> Control -> Lsa -> MSV1_0  as shown in below screen shot and edit the BackConnectionHostNames and specify fully qualified host name.



Once the registry entry is done, it is recommended to restart the server.

Step 6: Now you can create a web application in SharePoint 2010 from Central Administration and use the host header mywebapp.testdomain.com


Once the web application is created, you can create a root site collection. Now you can browse the web application by using the host header and specified port number (http://mywebapp.testdomain.com:port/)

Note: In your intranet if it still does'nt render the web application by browsing it by host adress, just make sure 1) you have entered the host address in your browser proxy 2) host address and IP address entry in host file on your local machine (Start->Run, type drivers and enter, click on etc folder you can see host file)

Thursday, May 24, 2012

How to track content changes of a publishing page in SharePoint 2010


Normally in SharePoint when you create a publishing page it goes through some approval cycle. During the approval cycle you check out the page and add/edit/remove some content and again check-in for further review. During this process reviewer would like to know what exactly is been modified in the content. Below steps shows how to track the changes between the versions of the page.

Problem Description:-
Let’s say you have created a publishing page and published it, again checkout it, modified, added some content, deleted some content and again published it. Now you want to see what has been changed between the versions.

Here are the steps to check the track changes:-

Step1: Click on View All Site Content from Site Actions menu display in top ribbon



Step 2: Click on the Pages link to open the pages library


Step3: Select the page which you have create and modified and want to see version changes. Below screen shot shows an example page title Testing versioning.



Step4: From the top ribbon click on Page option and then click on Page History as shown in below screen shot



Step5: Now you can see the track changes between latest version and the previous version in below screen shot.


As you can see the Versions in left section in the above screenshot, all versions will be automatically populated in drop down, you can select any version to compare and see track changes.

Tuesday, May 1, 2012

JavaScript error on page -_spbodyonloadfunctionnames is undefined


Concept:
As you know in master and content page concept, the HTML ‘body’  is defined in master page and the content page defines only the html part that need to be rendered in the main placeholder in the master page. Now from a content page if you want to execute some JavaScript function on the body load event, the function needs to be invoked in body load event, to do that SharePoint defines an array in which all the JavaScript function which need to be called on body load can be added. When the body is loaded the onload event handlers executes each functions added in the _spBodyOnLoadFunctionNames array.

How it works:
SharePoint 2010 uses lots of JavaScript’s to make rich user experience, in run time it loads many JavaScript’s functions and add them in the spBodyOnLoadFunctionNames to execute it on body load event.

It uses init.js file to handle such JavaScript’s. SharePoint adds the reference to init.js file on run time within the head tag. This reference must be added before adding any function in spBodyOnLoadFunctionNames.

Below is the code which shows how the init file reference is added once page is rendered


document.write('<script type="text/javascript" src="/_layouts/1033/init.js?rev=BJDmyeIV5jS04CPkRq4Ldg%3D%3D"></' + 'script>');


Below is one custom javascript function which I want to execute in body load event

<script type="text/javascript">  
function ApplyFormat()
{
var meetingWPDIV = document.getElementById("MeetingWebparts");
if (meetingWPDIV != null) {
if (meetingWPDIV.innerText.indexOf("This Web Part was designed for use on a Microsoft Meeting Workspace") < 0) {
meetingWPDIV.style.display = "";
}
}
}
_spBodyOnLoadFunctionNames.push('ApplyFormat');
</script>


Here is how body tag of master page looks like which handles the method calls using init.js file and based on the functions added in spBodyOnLoadFunctionNames.


<body scroll="no" onload="if (typeof(_spBodyOnLoadWrapper) != 'undefined') _spBodyOnLoadWrapper();" class="v4master">


The above JavaScript function ApplyFormat() is added in _spBodyOnLoadFunctionNames  array. Now when the page load will happen, on the body load event my function ApplyFormat()  Will be executed

Error cause & solution
  • Init.js file reference is missing , to resolve this, check the page source and make sure the init.js file reference is added In the page head tag
  • The JavaScript function which is added in the array  _spBodyOnLoadFunctionNames;  is written before the init.js file reference added in the page.
This is most common mistakes, we add the custom scripts on the top in the head tag and as you know the reference to init.js file is also added in the head tag, so if the custom method added in array _spBodyOnLoadFunctionNames  would not work because init.js file not yet referenced so it is not able to find the definition of _spBodyOnLoadFunctionNames  and so gives script error on the page.
  
To resolve this error, check the page source and see the line number where init.js reference is added, and now see the custom JavaScript _spBodyOnLoadFunctionNames   code line number, this should be used after the init.js reference is added.

  • The body tag element is not properly defined, check the page source and make sure the body tag is properly defined as given above.


Monday, April 30, 2012

Open SharePoint Dialog box using JavaScript

SharePoint 2010 provides a new feature Modal Dialogs box which is based on the JavaScript client object modal. This popup can open up a new page while you are on the current page, no need to redirect/navigate to the other page. In SharePoint OOB functionalities this popup is used mostly to perform any CRUD operation on the data. This dialog box provides an IFrame in which the data is displayed.

Below is the sample JavaScript function you can invoke from any client side event that will open the SharePoint modal dialog box.

function OpenBlogPortalDialog(pageURL) {
var NewPopUp = SP.UI.$create_DialogOptions();
NewPopUp.url = pageURL;
NewPopUp.width = 800;
          NewPopUp.height = 500;
          SP.UI.ModalDialog.showModalDialog(NewPopUp);  
    }

Below the screen shot shows how the popup looks like when is it opened on the current page when the submit button is clicked.



Users can drag the popup and can maximize the popup to full window; these behaviors of the popup can be set in the JavaScript code. 

Below are the various other properties which can be set in the JavaScript function for rendering the popup

Property Name
Type
Description
allowMaximize
boolean (true/false)
Determines the visibility of the maximize button (at top right corner) for the modal dialog
args
object
The args property allows us to pass arbitrary properties into our dialog.
autoSize
boolean (true/false)

dialogReturnValueCallback
function
This property accepts a callback function which gets executed when the dialog is closed.
height
numeric
The height of the dialog
html
HTML Element
The HTML to be rendered in the window (when the URL property is not specified)
showClose
boolean (true/false)
Determines the visibility of the close button (at top right corner) for the modal dialog
showMaximized
boolean (true/false)
If set to true, the dialog will render maximized, i.e. it fills the available screen space.
title
string
Title of the modal dialog. When no title is specified, the title of the document referred to by the Url property is used instead.
url
string
The URL of the page to be shown in the dialog.
width
numeric
The width of the dialog to be displayed
x
numeric
Specifies the starting position from the left, where the dialog is to be rendered
y
numeric
Specifies the starting position from the bottom, where the dialog is to be rendered

Sunday, April 29, 2012

SharePoint Performance - List.Items.Count Vs List.ItemCount


There are some common coding mistakes which severely hits the SharePoint performance. Let me take an example on List.Items.Count Vs List.ItemCount code.

Int itemCount = SPContext.Current.List.Items.Count;

The above code returns number of items from the list, to get the number of items it retrieves all items of the list from the content database and then returns the count. This code might not be a problem for a small list, normally during development we don’t have large data in the list so it doesn’t reflect the performance hit but when you have a large list, this code will become performance overhead.

There is a different property on the SPList which can be used in such scenario and will be better for performance.Here is the alternative to the above code

Int itemCount = SPContext.Current.List.ItemCount;

In above code, SharePoint just query a single record from the List table from the content database. There is a redundant column for number of items in the list which stores the count, so the above code just fetch the number of items without querying the entire data.

So the best practice is to use SPContext.Current.List.ItemCount if you want to fetch item count.

SharePoint 2010 Installation – Adhere to the security practice of least privilege


As the title of the this post says ‘Adhere to the security practice of least privilege’, this means that accounts used for SharePoint implementation should be created in such a way that it can be given only the permission required to perform its task. Many times people just create one or two accounts and use it for running all the services and installation of the SharePoint this can be acceptable in development environment but is definitely not a good practice for test/staging or production environment.

As you know SharePoint has close dependencies on, SQL Server and Active Directory. Active Directory stores user accounts and validates account logon and the services supports user logging on to the SharePoint sites whereas SQL Server stores almost all of the configurations and content of the SharePoint farm.

Here are the accounts setups which enable least privilege implementation of the SharePoint; you need to create these accounts before installing the SharePoint.

Setup Active Directory Accounts:
Start the Active Directory Users and Computers and in the Service Accounts create following user accounts

Users Accounts
Descriptions
SQL_Admin
SQL Server administrator account, this account need to be local admin on the SQL Server machine and use this account for installation of the SQL Server database.
SQL_Service
SQL Server service accounts, use this account for running MSSQLSERVER and SQLSERVERAGENT services
SP_Admin
SharePoint administrator and setup users, add this account in the DnsAdmins group of the domain and also in the local administrators group of the SharePoint server machine.
SP_Farm
SharePoint farm service
SP_ServiceApps
SharePoint service applications
SP_WebApps
SharePoint web applications
SP_Crawl
SharePoint search crawler
SP_UserSync
SharePoint user profile synchronization

Setup SQL Server login for SharePoint Administrator:
SP_Admin is the only account for which a SQL login must be manually created, so you need to connect to your SQL Server and open SQL Server Management Studio, create a login for SP_Admin in the SQL Server. Assign dbcreator and securityadmin servers roles to SP_Admin account

Once above account setups are done, you can proceed with SharePoint installation and use the above accounts during the installation and during the setup the services