Monday, July 18, 2011
Sharepoint Fundas
Creating the Web Part
This procedure demonstrates how to create an ASP.NET Web Part to wrap the user control.
To create the ASP.NET Web Part
- In Visual Studio, point to New on the File menu, and then click Project.
- In the Project types pane, click WSPBuilder. In the Templates pane, click WSPBuilder Project. In the Name box, type MyWebPart, and then click OK.
- Right-click the MyWebPart project, point to Add, and then click New Item…
- In the Categories pane, click WSPBuilder. In the Templates pane, click Web Part Feature. In the Name box, type MyWebPart, and then click OK.
- In the Feature Settings dialog, type My Web Part for the Title, A web part built using WSPBuilder for the Description and set the Scope to be Web. Click OK.
- Open the elements.xml file and edit the values for the Group and QuickAddGroups properties. The following code shows the corrected values:
- Right-click on the MyWebPart soluction, then select Build Solution. Note the PublicKeyToken for the compiled assembly.
- Optional: Add the following property to the Properties block in the MyWebPart.webpartfile to prevent users from closing the web part once it is on a page:
<Property Name="Group" Value="My Group"></Property> <Property Name="QuickAddGroups" Value="My Group" />
<property name="AllowClose" type="boolean">False</property>
Adding ASP.NET Project Types to the WSPBuilder Project
This procedure demonstrates how to add the ASP.NET web application templates to a WSPBuilder project. This is required to permit the use of the ASP.NET User Control within the WSPBuilder project.
To add the ASP.NET templates
- In Visual Studio, right-click on the MyWebPart project and select Unload Project.
- Right-click on the MyWebPart project again and select Edit MyWebPart.csproj. Locate the ProjectTypeGuids element and add the ASP.NET web application project type guid{349C5851-65DF-11DA-9384-00065B846F21}. The following code shows the corrected element:
- Save and then close the MyWebPart.csproj file.
- Right-click on the MyWebPart project and select Reload Project.
- Right-click on the MyWebPart project and select Properties On the Application tab, change the Target Framework to be .NET Framework 3.5. Save the changes.
<ProjectTypeGuids>{349C5851-65DF-11DA-9384-00065B846F21};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
Creating the ASP.NET User Control
This procedure demonstrates how to create an ASP.NET user control that uses SharePoint.
To create the ASP.NET user control
- In Visual Studio, right-click the TEMPLATE folder, select Add then click New Folder. Name the folder CONTROLTEMPLATES.
- Right-click the CONTROLTEMPLATES folder, select Add then click New Folder. Name the folder MyWebPart
- Right-click the MyWebPart folder, select Add then click New Item.
- In the Categories pane, click Web. In the Templates pane, click Web User Control. Name the control MyWebUserControl.ascx, and then click Add.
- Delete the CodeBehind attribute in the MyWebUserControl.ascx file. Replace theInherits attribute with MyWebPart.MyWebUserControl, MyWebPart, Version=1.0.0.0, Culture=neutral, PublicKeyToken=[your PublicKeyToken]. The following code shows the corrected file:
- Open the MyWebUserControl.ascx file in the designer view. Click the Toolbox, and then add a Label using a drag-and-drop operation. Switch to the code view and rename the IDproperty to be MyLabel.
- Open the MyWebUserControl.ascx.cs file and the MyWebUserControl.ascx.designer.cs file. Change the namespace to MyWebPart in both files.
- Open the MyWebUserControl.ascx.cs file. Add a public string property namedDisplayText to the MyWebUserControl class. The following code demonstrates this:
- Add the following code to the Page_Load method. This will display the value of the DisplayText property using the MyLabel control.
<%@ Control Language="C#" AutoEventWireup="true" Inherits="MyWebPart.MyWebUserControl, MyWebPart, Version=1.0.0.0, Culture=neutral, PublicKeyToken=9410bf5b454f3bbd" %>
public string DisplayText { get; set; }
protected void Page_Load(object sender, EventArgs e) { MyLabel.Text = DisplayText; }
Wrapping the User Control and Connecting the Properties
This procedure demonstrates how to wrap the user control inside the web part and connect the properties between SharePoint, the web wart and the user control.
To wrap the user control and connect the properties
- Open the MyWebPart.cs file.
- Edit the attributes of the MyProperty property. The code below shows the changes:
- Within the CreateChildControls() method, delete the single line of code under the
// Your code here...
comment. Enter code to load the MyWebUserControl, assign the value of MyProperty to the DisplayText property of the control and then add the control to the web part’s Controls collection. The code below demonstrates how to do this: - Right-click on the MyWebPart solution and select Rebuild Solution.
[Personalizable(PersonalizationScope.Shared)] [WebBrowsable(true)] [System.ComponentModel.Category("Custom")] [WebDisplayName("MyProperty")] [WebDescription("Message to be displayed within Web Part")]
// Your code here... MyWebUserControl myControl = (MyWebUserControl)Page.LoadControl("~/_controltemplates/MyWebPart/MyWebUserControl.ascx"); myControl.DisplayText = MyProperty; this.Controls.Add(myControl);
The completed solution structure is shown below:
Deploying the Web Part and Testing Functionality
This procedure demonstrates how to deploy the web part and the user control, and how to test their functionality. This procedure assumes that there is a local instance of SharePoint to deploy to.
To deploy the web part
- Right-click on the MyWebPart project, select WSPBuilder and then Build WSP. Follow the progress of the build in the Output window and wait for the process to complete.
- Right-click on the MyWebPart project, select WSPBuilder and then Deploy. Follow the progress of the build in the Output window and wait for the process to complete.
- Browse to the local SharePoint site.
- In the Site Actions drop-down box, click Site Settings. On the Site Settings page, click on the Site features link.
- Click on the Activate button next to the My Web Part feature.
- Click on the link to the default home page for the SharePoint site.
- In the Site Actions drop-down box, click Edit Page.
- Click Add a Web Part in one of the Web Part zones on the page, click the My Web PartWeb Part (located in the My Group section), and then click Add.
- Click Exit Edit Mode. You should now see the default “Hello SharePoint” message.
- To change the text displayed, click the drop-down box on the Web Part, and then clickModify Shared Web Part.
- Expand the Custom group, enter the new display message in the My Property box, and then click Apply.
- Click Exit Edit Mode. You should now see the new message.
Thanks to : WordPress.com
Suggestions are always welcome...! Without signing also you can send your comments.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment