Tuesday, December 28, 2010

SharePoint 2010 - Open a modal dialog from a web part

In SharePoint 2010, the New/Edit/Display form for lists appear as modal dialogs. This uses the Dialog Platform. The modal dialog concept is used when uploading files and also other places in SharePoint 2010.

We can use the same concept if we want to open a modal dialog from a web part. It is pretty simple!

In the web part code, add a prerender event handler:


public MyWebPart()
{
  this.PreRender += new EventHandler(MyWebPart_PreRender);
}


In the prerender handler, register the javascript to open the modal dialog:


private void MyWebPart_PreRender(object sender, System.EventArgs e)
{
  if (!Page.ClientScript.IsClientScriptBlockRegistered(OPEN_DIALOG_SCRIPT))
  {
    string includeScript = @"<script language='javascript'>function OpenDialog(formURL) {var options = SP.UI.$create_DialogOptions(); options.url = formURL; SP.UI.ModalDialog.showModalDialog(options); }</script>";

    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), OPEN_DIALOG_SCRIPT, includeScript);
  }
}


Now, we just need to associate and invoke the javascript OpenDialog function in a hyperlink or a button click. In the below example, I am creating a hyperlink in CreateChildControls method and invoking the function on the hyperlink click:


protected override void CreateChildControls()
{
  HyperLink addListItemLink = new HyperLink();
  addListItemLink.Attributes.Add("align", "left");
  addListItemLink.CssClass = "ms-sitemapdirectional";
  addListItemLink.Text = "Add New Item";
  addListItemLink.NavigateUrl = string.Format(@"javascript:OpenDialog('{0}');", sNavigateUrl);
  this.Controls.Add(addListItemLink);
}


In the above example, the URL in the sNavigateUrl will open in a modal dialog.

Wednesday, June 9, 2010

"An error has occured in approval of changes"

A business user setup the OOB Approval workflow for a document library. The Approval workflow completed fine. However, at the end of the workflow, it gave the "An error has occured in approval of changes" error! After a little bit of playing around, I figured that Content Approval for the document library needs to be enabled in order for the Approval workflow to complete successfully.

Steps to enable Content Approval are:
1. Go to Document Library settings.
2. Click on Versioning Settings under General Settings
3. Set "Require content approval for submitted items?" to Yes

Monday, May 24, 2010

Site limit in Navigation

By default, the quick launch and top navigation bar only show a maximum of 50 sub-sites. This is related to the sitemap's provider DynamicChildLimit property. Setting the DynamicChildLimit property value to "0" will remove this limit and the navigation bars will list the sub-sites.

This is the piece of the web.config with the DynamicChildLimit property set to 0:


< siteMap defaultProvider="CurrentNavSiteMapProvider" enabled="true" >
< providers >
....
....
....
< add name="GlobalNavSiteMapProvider" description="CMS provider for Global navigation" type="Microsoft.SharePoint.Publishing.Navigation.PortalSiteMapProvider, Microsoft.SharePoint.Publishing, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" NavigationType="Global" EncodeOutput="true" IncludePages="PerWeb" IncludeHeadings="true" IncludeAuthoredLinks="true" DynamicChildLimit="0" / >
< add name="CombinedNavSiteMapProvider" description="CMS provider for Combined navigation" type="Microsoft.SharePoint.Publishing.Navigation.PortalSiteMapProvider, Microsoft.SharePoint.Publishing, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" NavigationType="Combined" EncodeOutput="true" IncludePages="PerWeb" IncludeHeadings="true" IncludeAuthoredLinks="true" DynamicChildLimit="0" / >
< add name="CurrentNavSiteMapProvider" description="CMS provider for Current navigation" type="Microsoft.SharePoint.Publishing.Navigation.PortalSiteMapProvider, Microsoft.SharePoint.Publishing, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" NavigationType="Current" EncodeOutput="true" IncludePages="PerWeb" IncludeHeadings="true" IncludeAuthoredLinks="true" DynamicChildLimit="0" / >
< add name="CurrentNavSiteMapProviderNoEncode" description="CMS provider for Current navigation, no encoding of output" type="Microsoft.SharePoint.Publishing.Navigation.PortalSiteMapProvider, Microsoft.SharePoint.Publishing, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" NavigationType="Current" EncodeOutput="false" IncludePages="PerWeb" IncludeHeadings="true" IncludeAuthoredLinks="true" DynamicChildLimit="0" / >
....
....
....
< /providers >
< /siteMap >

Friday, February 12, 2010

Access denied when copying file to GAC

I was trying to copy a dll to GAC on a Windows Server 2008 machine and kept getting "Access Denied" message.

After fighting with the message for a while and playing with some settings figured the solution. Turning off User Access Control solves the problem. Steps to turn off User Access Control are:
1. Go to Control Panel --> User Accounts
2. Click on "Turn User Account Control On or Off" link
3. Uncheck "Use User Account Control(UAC) to help protect your computer" and Click OK.