Create custom modal dialog using Visual studio 2010 in SharePoint 2010

Below are the detailed steps to create a custom modal dialog page and show it when a ribbon button is clicked in a document library.

1. User clicks on Ribbion button “About” in the document library.

2. About button the displays the custom dialog page and passes the current library name.

3. The dialog page then parses the url parameter, Library name in our case and display the description field as text in the dialog page.

Results -

Ribbon Button

Create custom modal dialog using Visual studio 2010 in SharePoint 2010

About Dialog -

Create custom modal dialog using Visual studio 2010 in SharePoint 2010

Steps –

1. Create a empty project “Aboutmodaldialog”.

2. Deploy it as a Farm solution.

3. Right click on the feature and click “Add feature”.

4. Next right click on project and add a new item and select an application page “AboutDialogPage.aspx”. This will be out cutsom dialog page.

5. Open the AboutDialogPage.aspx page and add a lablel “AboutText” in PlaceHolderMain.

6. Next, write the below code in the applictaion page to display the About text for a library.

using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace AboutModalDialog.Layouts.AboutModalDialog
{
public partial class AboutDialogPage : LayoutsPageBase
{
protected void Page_Load(object sender, EventArgs e)
{
SPWeb web = SPContext.Current.Web; //here, because we use the SPContext, so we don't need to dispose SPWeb, or
//use the using

if (Request.QueryString["sourcelibrary"] != null)
{

String CurrentListName = Request.QueryString["sourcelibrary"].ToString();

SPList _currentList = web.Lists[CurrentListName];

AboutText.Text = string.Format("About This Library {0}", _currentList.Description);

}

}
}
}







About why we should not need to dispose SPWeb or use the using, you could take a look these 2 articles:



The Best Practice Guidelines of Development on SharePoint 2010/SharePoint 2007 - part 1



The Best Practice Guidelines of Development on SharePoint 2010/SharePoint 2007 – Part 2



You Project should look like -



Create custom modal dialog using Visual studio 2010 in SharePoint 2010



7. Next we will create an empty element to display our custom ribbon button “About” which when clicked will open up



8. Next step would be to add a new “Empty Element” type item in the project. See below


9. Add the below code in the Element file

Create custom modal dialog using Visual studio 2010 in SharePoint 2010




<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<CustomAction
Id="AboutFilesButton"
Location="CommandUI.Ribbon"
RegistrationId="101"
RegistrationType="List"
Title="About">
<CommandUIExtension>
<CommandUIDefinitions>
<CommandUIDefinition
Location="Ribbon.Library.ViewFormat.Controls._children">
<Button Id="Ribbon.Library.ViewFormat.About"
Command="AboutButtonCommand"
LabelText="About"
Image32by32="/_layouts/1033/IMAGES/APPROVE.GIF"
TemplateAlias="o1" />
</CommandUIDefinition>
</CommandUIDefinitions>
<CommandUIHandlers>
<CommandUIHandler
Command="AboutButtonCommand"
CommandAction="javascript:
var context = SP.ClientContext.get_current();
var web = context.get_web();
context.load(web);
var currentlibid = SP.ListOperation.Selection.getSelectedList();
var currentLib = web.get_lists().getById(currentlibid);
context.load(currentLib);
context.executeQueryAsync(
function (sender, args) {
if(currentLib != null) {
var options = {
url: '/_layouts/AboutModalDialog/AboutDialogPage.aspx?sourcelibrary='+ currentLib.get_title(),
tite: 'About',
allowMaximize: false,
showClose: true,
width: 400,
height: 400,
};
SP.UI.ModalDialog.showModalDialog(options);
}
},
function (sender, args) {
alert('Error occured' + args.get_message());}
);
"
/>
</CommandUIHandlers>
</CommandUIExtension>
</CustomAction>
</Elements>






10. Next, build and deploy your project. At this point you should see a “About” button in your document library ( under Library tab) which when clicked will display your custom modal dialog.