13
powering the presentation layer www.infragistics.com Infragistics Sales 800 231 8588 Using NetAdvantage 2005 Volume 2 elements in Windows Sharepoint Services Web Parts Microsoft Windows Sharepoint Services (WSS) is a powerful web-based portal package that many companies have adopted as a means of communication and collaboration in their organization. WSS works by presenting users a series of Web Parts organized into a single web page. Web Parts deliver the necessary information to end users, and allow them to create customized interfaces containing the key information unique to each user. Web Parts are also a key feature of WSS easy to use extensibility model. This model gives developers the power to create their own custom Web Parts using ASP.NET and plug them directly into the WSS framework. Developers can utilize the power of the NetAdvantage ASP.NET elements to create highly customized, and powerful custom Web Parts for their WSS based collaboration portals. This article shows you how you can create a simple Web Part that contains elements from the NetAdvantage 2005 Volume 2 toolset, and then deploy the Web Part to a WSS server. Creating a Simple Web Part Creating custom Web Parts for WSS is a simple task, consisting of two basic steps: creating a Web Part Library in Visual Studio .NET, and deploying the library to your WSS server. Before starting to create your first Web Part, it is recommended that you download and install the Sharepoint Web Part Templates for Visual Studio .NET. These Visual Studio project templates greatly simply creating Web Parts by providing you with a complete stub class from which you can start creating your custom Web Part. http://www.microsoft.com/downloads/details.aspx?FamilyID=14D5D92F-C3A6-407C-AAD7-B8C41A4991BE&displaylang=en In addition to installing the Templates, before you get started with this articles sample, it is recommended that you read and understand the Creating a Basic Web Part topic on the MSDN website. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/spptsdk/html/CreateABasicWP_SV01003709.asp In this article, you will see how to build a simple Web Part that adds several different server controls, including a TextBox, Button, Calendar and UltraWebTree control to the Web Part. The UltraWebTree will be populated with several sample nodes. To get started, simply select the Web Part Library from Visual Studio. This project template includes a stub Web Part class, as well as the DWP file used to load the Web Part into Sharepoint.

powering the presentation layer Infragistics Sales 800 231 8588 Using NetAdvantage™ 2005 Volume 2 elements in Windows Sharepoint Services Web Parts Microsoft Windows Sharepoint Services

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: powering the presentation layer Infragistics Sales 800 231 8588 Using NetAdvantage™ 2005 Volume 2 elements in Windows Sharepoint Services Web Parts Microsoft Windows Sharepoint Services

po

wer

ing

the

pre

sent

atio

n la

yer

www.infragistics.comInfragistics Sales 800 231 8588

Using NetAdvantage™ 2005 Volume 2 elements inWindows Sharepoint Services Web PartsMicrosoft Windows Sharepoint Services (WSS) is a powerful web-based portal package that many companieshave adopted as a means of communication and collaboration in their organization. WSS works by presentingusers a series of Web Parts organized into a single web page. Web Parts deliver the necessary information toend users, and allow them to create customized interfaces containing the key information unique to each user.Web Parts are also a key feature of WSS easy to use extensibility model. This model gives developers thepower to create their own custom Web Parts using ASP.NET and plug them directly into the WSS framework.

Developers can utilize the power of the NetAdvantage ASP.NET elements to create highly customized, andpowerful custom Web Parts for their WSS based collaboration portals. This article shows you how you cancreate a simple Web Part that contains elements from the NetAdvantage 2005 Volume 2 toolset, and thendeploy the Web Part to a WSS server.

Creating a Simple Web Part

Creating custom Web Parts for WSS is a simple task, consisting of two basic steps: creating a Web Part Libraryin Visual Studio .NET, and deploying the library to your WSS server.

Before starting to create your first Web Part, it is recommended that you download and install the SharepointWeb Part Templates for Visual Studio .NET. These Visual Studio project templates greatly simply creating WebParts by providing you with a complete stub class from which you can start creating your custom Web Part.

http://www.microsoft.com/downloads/details.aspx?FamilyID=14D5D92F-C3A6-407C-AAD7-B8C41A4991BE&displaylang=en

In addition to installing the Templates, before you get started with this articles sample, it is recommended thatyou read and understand the Creating a Basic Web Part topic on the MSDN website.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/spptsdk/html/CreateABasicWP_SV01003709.asp

In this article, you will see how to build a simple Web Part that adds several different server controls, including aTextBox, Button, Calendar and UltraWebTree control to the Web Part. The UltraWebTree will be populatedwith several sample nodes.

To get started, simply select the Web Part Library from VisualStudio. This project template includes a stub Web Part class, aswell as the DWP file used to load the Web Part into Sharepoint.

Page 2: powering the presentation layer Infragistics Sales 800 231 8588 Using NetAdvantage™ 2005 Volume 2 elements in Windows Sharepoint Services Web Parts Microsoft Windows Sharepoint Services

po

wer

ing

the

pre

sent

atio

n la

yer

www.infragistics.comInfragistics Sales 800 231 8588

The following code listing demonstrates how to create the sample Web Part.

VB

Imports SystemImports System.ComponentModelImports System.Web.UIImports System.Web.UI.WebControlsImports System.Xml.SerializationImports Microsoft.SharePointImports Microsoft.SharePoint.UtilitiesImports Microsoft.SharePoint.WebPartPages

’Description for WebPart1.<DefaultProperty("Text"), _

ToolboxData("<{0}:WebPart1 runat=server></{0}:WebPart1>"), _XmlRoot(Namespace:="WebPartLibrary2")> _

Public Class WebPart1 Inherits Microsoft.SharePoint.WebPartPages.WebPart

Private Const _defaultText As String = ""

Private table As TablePrivate webtree As Infragistics.WebUI.UltraWebNavigator.UltraWebTreePrivate textbox As TextBoxPrivate button As ButtonPrivate calendar As Calendar

Dim _text As String = _defaultText

<Browsable(True), Category("Miscellaneous"), _DefaultValue(_defaultText), WebPartStorage(Storage.Personal), _FriendlyName("Text"), Description("Text Property")> _Property [Text]() As String

GetReturn _text

End Get

Set(ByVal Value As String)_text = Value

End SetEnd Property

’Render this Web Part to the output parameter specified.Protected Overrides Sub RenderWebPart( _

ByVal output As System.Web.UI.HtmlTextWriter)

Page 3: powering the presentation layer Infragistics Sales 800 231 8588 Using NetAdvantage™ 2005 Volume 2 elements in Windows Sharepoint Services Web Parts Microsoft Windows Sharepoint Services

po

wer

ing

the

pre

sent

atio

n la

yer

www.infragistics.comInfragistics Sales 800 231 8588

Tryoutput.Write(SPEncode.HtmlEncode(Text))

’Render the individual controls being added to the Web Formtable.RenderControl(output)

Catch exc As Exceptionoutput.Write(exc.Message + exc.Source + exc.StackTrace)

End TryEnd Sub

Protected Overrides Sub CreateChildControls()

MyBase.CreateChildControls()

table = New Tabletable.GridLines = GridLines.Bothtable.BorderWidth = Unit.Pixel(0)table.Width = Unit.Percentage(100)table.Height = Unit.Percentage(100)

table.Rows.Add(New TableRow)table.Rows.Add(New TableRow)

table.Rows(0).Cells.Add(New TableCell)table.Rows(0).Cells.Add(New TableCell)table.Rows(1).Cells.Add(New TableCell)table.Rows(1).Cells.Add(New TableCell)

’Add a WebTree to the Controls collectionwebtree = New Infragistics.WebUI.UltraWebNavigator.UltraWebTreewebtree.ID = "UltraWebTree1"webtree.ExpandImage = "ig_treePlus.gif"webtree.CollapseImage = "ig_treeMinus.gif"table.Rows(0).Cells(0).Width = Unit.Pixel(120)table.Rows(0).Cells(0).VerticalAlign = VerticalAlign.Toptable.Rows(0).Cells(0).Controls.Add(webtree)

’Add a TextBox to the Controls collectiontextbox = New TextBoxtextbox.ID = "TextBox1"table.Rows(1).Cells(1).Controls.Add(textbox)

’Add a Button to the Controls collectionbutton = New Buttonbutton.ID = "Button1"

Page 4: powering the presentation layer Infragistics Sales 800 231 8588 Using NetAdvantage™ 2005 Volume 2 elements in Windows Sharepoint Services Web Parts Microsoft Windows Sharepoint Services

button.Text = "Submit"table.Rows(1).Cells(1).Controls.Add(button)

’Add a Calendar to the Controls collectioncalendar = New Calendarcalendar.ID = "Calendar1"calendar.SelectedDate = System.DateTime.Nowtable.Rows(0).Cells(1).Controls.Add(calendar)

If (Not Page.IsPostBack) Then

’Add several tree nodes to the WebTree’You could also create the nodes by binding’ the tree to a data source, or by enumerating’ a collection and recursivly adding nodesDim node As Infragistics.WebUI.UltraWebNavigator.Node

node = webtree.Nodes.Add("Root1")node.Nodes.Add("Child1")

node = webtree.Nodes.Add("Root2")node.Nodes.Add("Child2")

node = webtree.Nodes.Add("Root3")node.Nodes.Add("Child3")

node = webtree.Nodes.Add("Root4")node.Nodes.Add("Child4")

End If

Controls.Add(table)End Sub

End Class

po

wer

ing

the

pre

sent

atio

n la

yer

www.infragistics.comInfragistics Sales 800 231 8588

Page 5: powering the presentation layer Infragistics Sales 800 231 8588 Using NetAdvantage™ 2005 Volume 2 elements in Windows Sharepoint Services Web Parts Microsoft Windows Sharepoint Services

po

wer

ing

the

pre

sent

atio

n la

yer

www.infragistics.comInfragistics Sales 800 231 8588

C#

using System;using System.ComponentModel;using System.Web.UI;using System.Web.UI.WebControls;using System.Xml.Serialization;using Microsoft.SharePoint;using Microsoft.SharePoint.Utilities;using Microsoft.SharePoint.WebPartPages;

namespace WebPartLibrary1{

/// <summary>/// Description for WebPart1./// </summary>[DefaultProperty("Text"),

ToolboxData("<{0}:WebPart1 runat=server></{0}:WebPart1>"),XmlRoot(Namespace="WebPartLibrary1")]

public class WebPart1 : Microsoft.SharePoint.WebPartPages.WebPart{

private const string defaultText = "";

private Table table;private Infragistics.WebUI.UltraWebNavigator.UltraWebTree webtree;private TextBox textbox;private Button button;private Calendar calendar;

private string text = defaultText;

[Browsable(true),Category("Miscellaneous"),DefaultValue(defaultText),WebPartStorage(Storage.Personal),FriendlyName("Text"),Description("Text Property")]

public string Text{

get{

return text;}set{

text = value;}

}

Page 6: powering the presentation layer Infragistics Sales 800 231 8588 Using NetAdvantage™ 2005 Volume 2 elements in Windows Sharepoint Services Web Parts Microsoft Windows Sharepoint Services

/// <summary>/// Render this Web Part to the output parameter specified./// </summary>/// <param name="output"> The HTML writer to write out to </param>protected override void RenderWebPart(HtmlTextWriter output){

try{

output.Write(SPEncode.HtmlEncode(Text));

//Render the individual controls being added to the Web Formtable.RenderControl(output);

}catch (Exception exc){

output.Write(exc.Message);}

}

/// <summary>/// /// </summary>protected override void CreateChildControls(){

base.CreateChildControls ();

table = new Table();table.GridLines=GridLines.Both;table.BorderWidth=Unit.Pixel(0);table.Width=Unit.Percentage(100);table.Height=Unit.Percentage(100);

table.Rows.Add(new TableRow());table.Rows.Add(new TableRow());

table.Rows[0].Cells.Add(new TableCell());table.Rows[0].Cells.Add(new TableCell());table.Rows[1].Cells.Add(new TableCell());table.Rows[1].Cells.Add(new TableCell());

//Add a WebTree to the Controls collectionwebtree=new Infragistics.WebUI.UltraWebNavigator.UltraWebTree();webtree.ID="UltraWebTree1";webtree.ExpandImage="ig_treePlus.gif";webtree.CollapseImage="ig_treeMinus.gif";table.Rows[0].Cells[0].Width=Unit.Pixel(120);

po

wer

ing

the

pre

sent

atio

n la

yer

www.infragistics.comInfragistics Sales 800 231 8588

Page 7: powering the presentation layer Infragistics Sales 800 231 8588 Using NetAdvantage™ 2005 Volume 2 elements in Windows Sharepoint Services Web Parts Microsoft Windows Sharepoint Services

table.Rows[0].Cells[0].VerticalAlign=VerticalAlign.Top;table.Rows[0].Cells[0].Controls.Add(webtree);

//Add a TextBox to the Controls collectiontextbox=new TextBox();textbox.ID="TextBox1";table.Rows[1].Cells[1].Controls.Add(textbox);

//Add a Button to the Controls collectionbutton=new Button();button.ID="Button1";button.Text="Submit";table.Rows[1].Cells[1].Controls.Add(button);

//Add a Calendar to the Controls collectioncalendar=new Calendar();calendar.ID="Calendar1";calendar.SelectedDate=System.DateTime.Now;table.Rows[0].Cells[1].Controls.Add(calendar);

if (!Page.IsPostBack){

//Add several tree nodes to the WebTree//You could also create the nodes by binding// the tree to a data source, or by enumerating// a collection and recursivly adding nodesInfragistics.WebUI.UltraWebNavigator.Node node;

node=webtree.Nodes.Add("Root1");node.Nodes.Add("Child1");

node=webtree.Nodes.Add("Root2");node.Nodes.Add("Child2");

node=webtree.Nodes.Add("Root3");node.Nodes.Add("Child3");

node=webtree.Nodes.Add("Root4");node.Nodes.Add("Child4");

}Controls.Add(table);

}}

}

po

wer

ing

the

pre

sent

atio

n la

yer

www.infragistics.comInfragistics Sales 800 231 8588

Page 8: powering the presentation layer Infragistics Sales 800 231 8588 Using NetAdvantage™ 2005 Volume 2 elements in Windows Sharepoint Services Web Parts Microsoft Windows Sharepoint Services

po

wer

ing

the

pre

sent

atio

n la

yer

www.infragistics.comInfragistics Sales 800 231 8588

Once you enter the code for the Web Part you need to create a Strong Name Key for the assembly. WSS requiresthat all Web Part assemblies it hosts be strongly named. To create a key for your Web Part assembly, simply usethe command line sn.exe utility included with the .NET SDK.

sn -k c:\keypair.snk

Once you have created the key, you need to modify the AssemblyKeyFile attribute found in the AssemblyInfo.csfile included in your Web Part Library project.

[assembly: AssemblyKeyFile("c:\\keypair.snk")]

Once this attribute has been modified, the next time you build the web Part Library project, the resulting assemblywill contain a strong name, allowing you to deploy it to Sharepoint.

Configuring Sharepoint 2003

Once you have completed creating your WSS Web Part assembly, you need to configure your WSS server so thatthe assembly can be deployed and executed properly. There are several different configuration file changes thatyou will need to make, as well as configuring Sharepoint so that the client side JavaScript and image files used bythe NetAdvantage elements can be accessed properly.

Deploying and configuring NetAdvantage resources

Because the sample Web Part created in the prior section utilizes an element from the NetAdvantage toolset (theUltraWebTree), the first step in deploying your Web Part is to configure the client script files and images used bythe NetAdvantage elements. To do this, you simply need to copy the required script and image files to the WSSserver. Once that is completed you will create a new virtual directory in IIS and configure the Virtual Directory inSharepoint.

Using the NetAdvantage Installation to Configure Scripts and Images

The easiest way to ensure that these files are configured properly is to simply run the NetAdvantage installationon the Sharepoint Services server. If you choose to do this, it is recommended that you select the custominstallation option and remove the Help and Samples options from the installation.

Manual Installation of NetAdvantage Scripts and Images

Should you decided to deploy the files to your WSS server manually, you simply need to create a location withinIIS that contains the necessary scripts and images that the NetAdvantage controls can access. There are two waysto do this.

Page 9: powering the presentation layer Infragistics Sales 800 231 8588 Using NetAdvantage™ 2005 Volume 2 elements in Windows Sharepoint Services Web Parts Microsoft Windows Sharepoint Services

1. Create a folder called ig_common in your web root (usually c:\inetpub\wwwroot\).

2. Create a Virtual Directory in the root of IIS called ig_common. Creating a Virtual Directory allows you to place the script and images files anywhere on your server.

IIS can contain several different WSS related websites, each containing several different web.config files, so youneed to make sure you are editing the proper file. The web.config file you want to edit is located under the rootof the IIS site that contains your WSS virtual server site. You do not want to edit the web.config files in theSharepoint Central Administration site.

po

wer

ing

the

pre

sent

atio

n la

yer

www.infragistics.comInfragistics Sales 800 231 8588

Page 10: powering the presentation layer Infragistics Sales 800 231 8588 Using NetAdvantage™ 2005 Volume 2 elements in Windows Sharepoint Services Web Parts Microsoft Windows Sharepoint Services

Once you have created the ig_common directory (or Virtual Directory), locate thec:\inetpub\wwwroot\aspnet_client\infragistics directory on your development system. Installed by theNetAdvantage installation program, this folder contains the image and script files needed by the NetAdvantageASP.NET elements.

Exploring the directory, you will find a single Images directory, as well as a separate directory containing theJavaScript files for every version of NetAdvantage you have installed. The script folder names follow the patternYYYYR, a four digit year combined with a release number (i.e 20053)

To deploy the resources, simply copy the Images directory and the appropriate script version directory from yourdevelopment system to the ig_common directory on your WSS server. For example, if you are currently usingNetAdvantage 2005 Volume 2, then you would copy the Images directory and the 20052 directory.

Configuring an Excluded Directory

Finally you need to configure the ig_common directory (or Virtual Directory) as an Excluded Directory inSharepoint. By default, Sharepoint will try to take over every incoming web request, including requests to theig_common directory. Configuring the directory as an Excluded directory, prevents Sharepoint from interceptingrequests made to it.

1. On the server that is running Sharepoint Services, click Start, click Administrative Tools, then click Sharepoint Central Administration

2. In the Virtual Server Configuration area, click Configure virtual server settings3. Form the Virtual Server List, click the link of the virtual server that you want to add the excluded paths to4. Under Virtual Server Management, click Define Managed Paths5. In the Add a New Path section, type the path to exclude in the Path box6. Click the Exclude path option 7. Click the OK button at the bottom of the page

When you have completes the steps, the newly added excluded path will be displayed in the Excluded Paths list

po

wer

ing

the

pre

sent

atio

n la

yer

www.infragistics.comInfragistics Sales 800 231 8588

Page 11: powering the presentation layer Infragistics Sales 800 231 8588 Using NetAdvantage™ 2005 Volume 2 elements in Windows Sharepoint Services Web Parts Microsoft Windows Sharepoint Services

Deploying the Web Part Assemblies

Once you have completed deploying the required NetAdvantageassets, you can now deploy your web parts main and referencedassemblies. To do this simply copy the Web Parts main assemblyand all referenced NetAdvantage assemblies to the Bin directoryof your Sharepoint Services Virtual Server. If no Bin directoryexists in your virtual server simply create one.

Configuring Web Part Assembly Security

By default WSS uses a very low level of trust for .NET assemblies. In order to complete the deployment of yoursample Web Part, you need to configure WSS so that it will consider the Web Parts assembly as safe, and thengrant the assembly a higher level of trust. These configuration changes are made in the web.config file locate inthe WSS virtual server root.IIS generally contains several different WSS related websites, each containing several different web.config files, soyou need to make sure you are editing the proper file. The web.config file you want to edit is located under theroot of the IIS site that contains your WSS virtual server site. You do not want to edit the web.config files in theSharepoint Central Administration site.

Declaring your assembly a SafeControl

In order to execute the Web Part assembly, WSS requires that you register it and its namespace as a SafeControlin the web.config file. To do this, simple add a new <SafeControl> block to the web.config.

<SafeControlAssembly="WebPartLibrary1, Version=1.0.0.0, Culture=neutral,

PublicKeyToken=def148956c61a16b"Namespace="WebPartLibrary1"TypeName="*"Safe="True"

/>

Replace the PublicKeyToken value (def148956c61a16b) in the above sample with the actual value for your WebPart's assembly. To determine the correct PublicKeyToken value for the Assembly attribute of the <SafeControl>tag for your Web Part, use the sn.exe command-line utility:

sn.exe -T c:\inetpub\wwwroot\bin\WebPartLibrary1.dll

po

wer

ing

the

pre

sent

atio

n la

yer

www.infragistics.comInfragistics Sales 800 231 8588

Page 12: powering the presentation layer Infragistics Sales 800 231 8588 Using NetAdvantage™ 2005 Volume 2 elements in Windows Sharepoint Services Web Parts Microsoft Windows Sharepoint Services

po

wer

ing

the

pre

sent

atio

n la

yer

www.infragistics.comInfragistics Sales 800 231 8588

Setting the Assembly Trust level

Locate or add the <trust> node in the web config file and change the level attribute to Full. WSS uses the .NETFrameworks built in Code Access Security (CAS) model to determine the trust level of your web parts assembly,and what access rights it should have. By changing the Trust mode of your WSS to Full, you are explicitly grantingany web part you add full CAS trust.

<trust level="Full" originUrl="" />.

Should you not want to change the trust level of your entire virtual server to Full, there are other ways ofconfiguring web part security. First, you can create a policy for your web parts assembly specifying the CAS rightsrequired to execute the assembly. Second you can install your web part assembly into the Global Assembly Cache(GAC). Installing an assembly into the GAC automatically grants the assembly full trust.For more information on this topic, please refer to the article Microsoft Windows Sharepoint Services and CodeAccess Security, on the Microsoft MSDN website.

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/odc_SP2003_ta/html/sharepoint_wsscodeaccesssecurity.asp

Using the Web Part in Sharepoint

Once you have completed configuring Sharepoint and deploying your webpart, you simply need to add the Web Part to the Web Part Gallery in yourSharepoint site. To do this, open the Modify Shared Page menu and selectAdd Web Parts > Import.

This will display an Import web form that allows you to upload a web partdefinition to the Web Parts Gallery. Your web part definition file is the DWPfile that was created as part of the Visual Studio Web Part Library projecttemplate. To add the definition, click the Browse button and locate andselect the WebPart1.dwp file that was created as part of your projecttemplate. Once you have selected the file, simply click the Upload buttonon the web forms to upload the definition into the Web Parts Gallery. Thenewly uploaded web part will be displayed below the form Import form.

Now that you have added the Web Part to the Gallery, you can simply dragthe web part into one of the Sharepoint content areas, as shown on the right.

Once you drop the web part into the content area, it will begin to render.

Page 13: powering the presentation layer Infragistics Sales 800 231 8588 Using NetAdvantage™ 2005 Volume 2 elements in Windows Sharepoint Services Web Parts Microsoft Windows Sharepoint Services

po

wer

ing

the

pre

sent

atio

n la

yer

www.infragistics.comInfragistics Sales 800 231 8588

Conclusion

This whitepaper has demonstrated how you can use tools from the NetAdvantage 2005 Volume 2 toolkit withincustom Windows Sharepoint Services web parts. Completing these simple steps, including proper serverconfiguration and deployment allow you to create rich web parts using the powerful tools contained in theNetAdvantage toolkit.