DCSIMG
February 2009 - Posts - Doron Goldberg

February 2009 - Posts

As already known, settings forms of MOSS 2007 can usually be found at:

C:\Program Files\Common Files\microsoft shared\Web Server Extensions\12\TEMPLATE\LAYOUTS

Each form (*.aspx file) can be accessed from any site on the portal inside /_layouts folder.

for example:

http://doron-g/_layouts/settings.aspx

and

http://doron-g/SiteDirectory/wiki/_layouts/settings.aspx

Each showing the same form, exposing enabled featured for each site.

In the following posts I will demonstrate how to add new forms to the _layouts folder and how to update (change) an existing form, both intended to extend share point elements to our needs.

In this first example I will upgrade the ‘Title, Description and Icon’ site settings form to be ‘Site Type, title, Description and Icon’ so each site will now have a new setting field called ‘Site Type’. (in this example it will be drop down list for the simplicity of things, but it can be any thing you like – from a drop down list to a complex user control)

The settings page is located in this address (can be accessed from the Site Settings):

http://doron-g/_layouts/prjsetng.aspx

image

CAUTION: The next steps will include changes in the prjsetng.aspx file – I advise you to keep copies of every file you change manually.

Open the prjsetng.aspx file with any text editor and add the required code for generating the new “Site Type” section in the form:

<wssuc:InputFormSection
	Title="Site Type"
	Id="idSiteType"
	runat="server"
	>
	<Template_Description>
		<asp:literal runat="server" text="Select site type" />
		 
	</Template_Description>
	<Template_InputFormControls>
		<wssuc:InputFormControl LabelText="Site type:" runat="server">
			<Template_Control>
				<TABLE border="0" cellpadding="0" cellspacing="0" dir="ltr">
				<TR nowrap>
					<TD class="ms-authoringcontrols">
				
<asp:DropDownList runat="server" ID="SiteType" Width="265">
<asp:ListItem Text="Normal Site" Value="1"></asp:ListItem>
<asp:ListItem Text="Special Site" Value="2"></asp:ListItem>
<asp:ListItem Text="Very Special Site" Value="ccc"></asp:ListItem>
</asp:DropDownList>
					</TD>
				</TR>
				</TABLE>
			</Template_Control>
		</wssuc:InputFormControl>
	</Template_InputFormControls>
</wssuc:InputFormSection>

This will generate the required drop down list with 3 options for our new site type property.

Now we need to add the required code behind for saving the new field as part of the site properties.

In order to do that we need to override the existing class which is handling the form with a new class of our own.

To identify the current class we need to examine the original .aspx file’s header:

<%@ Assembly Name="Microsoft.SharePoint.ApplicationPages, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c"%> 
<%@ Page Language="C#" Inherits="Microsoft.SharePoint.ApplicationPages.ProjectSettingsPage" MasterPageFile="~/_layouts/application.master"      %>

These two lines tell us which class is used from which assembly. In this case ProjectSettingsPage from Microsoft.ShrePoint.ApplicationPages.

Our new custom ProjectSettingsPage class will look like this (inside a simple class library project)

    public class ProjectSettingsPage : Microsoft.SharePoint.ApplicationPages.ProjectSettingsPage
    {
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            if (!this.Page.IsPostBack)
            {
                if (base.Web.Properties.ContainsKey("SiteType"))
                {
                    
                    this.SiteType.SelectedValue = base.Web.Properties["SiteType"];
                }  
            }
        }
         public TextBox TxtSiteType { get; set; }
         public DropDownList SiteType { get; set; }
         protected void BtnUpdateWebNew_Click(object sender, EventArgs e)
         {
             if (!base.Web.Properties.ContainsKey("SiteType"))
             {
                
                 base.Web.Properties.Add("SiteType", this.SiteType.SelectedValue);
             }
             else
             {
                 
                 base.Web.Properties["SiteType"] = this.SiteType.SelectedValue;
             }
            
             base.Web.Properties.Update();
             BtnUpdateWeb_Click(sender, e);
         }
     
    }

This code is simple asp.net code which saves the new property as site property and / or initialize the control with a default value (As seen in the OnLoad function).

BtnUpdateWebNew_Click is for the OnClick event which will be generated by the OK button which saves the changes made.

In order to connect the existing button with the new method another change is required in the .aspx file:

 <wssuc:ButtonSection runat="server">
	   <Template_Buttons>
		  <asp:Button UseSubmitBehavior="false" runat="server"
		   class="ms-ButtonHeightWidth" OnClick="BtnUpdateWebNew_Click" 
		   Text="<%$Resources:wss,multipages_okbutton_text%>" id="BtnCreate" 
		   accesskey="<%$Resources:wss,okbutton_accesskey%>"/>
	   </Template_Buttons>
 </wssuc:ButtonSection>

All we need to do is change the OnClick attribute to point the new method we created.

(Our new method is saving the selected value of our new property and then calls the base class’s onclick method in order to save the original items of the form)

In order to set the complete form to use our new class the following changes are rquired in the .aspx page header:

<%@ Assembly Name="Netwise.MOSS.CustomPages, Version=1.0.0.0, Culture=neutral, PublicKeyToken=ef72d5cefb9f703d"%>
<%@ Page Language="C#" Inherits="Netwise.MOSS.CustomPages.ProjectSettingsPage" MasterPageFile="~/_layouts/application.master"      %>

So now our form is using our new class from out new assembly.

Just copy the new .aspx file to the layouts folder and place the assembly in the GAC. (make sure you use the right publickeytoken, version and culture when you alter the page header)

Our new settings form now looks like this:

image

In the next post I will demonstrate how to create a new settings form so you won’t have to worry about changing the original forms and also have the ability to create custom settings sections for you MOSS application.

Posted by dorong | 6 comment(s)
תגים:, , ,