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;

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
boolean (true/false)
Determines the visibility of the maximize button (at top right corner) for the modal dialog
The args property allows us to pass arbitrary properties into our dialog.
boolean (true/false)

This property accepts a callback function which gets executed when the dialog is closed.
The height of the dialog
HTML Element
The HTML to be rendered in the window (when the URL property is not specified)
boolean (true/false)
Determines the visibility of the close button (at top right corner) for the modal dialog
boolean (true/false)
If set to true, the dialog will render maximized, i.e. it fills the available screen space.
Title of the modal dialog. When no title is specified, the title of the document referred to by the Url property is used instead.
The URL of the page to be shown in the dialog.
The width of the dialog to be displayed
Specifies the starting position from the left, where the dialog is to be rendered
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
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 Server service accounts, use this account for running MSSQLSERVER and SQLSERVERAGENT services
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.
SharePoint farm service
SharePoint service applications
SharePoint web applications
SharePoint search crawler
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