Web Parts. How to add web part to a page.

by Kai 5. July 2011 22:37

Hi!

In today's post about web parts I'm going to show how you can add web part to a page. Previos posts:

You can add a web part using one of two methods - using declarative approach (AllUsersWebPart element) or through code.

When you use the first approach you can specify WebPartZoneID and WebPartOrder. In <![CDATA[]]> you must place web part's layout (it can be copied from .webpart file). You can also specify all required standart (or your custom) web part properties:

<Module Name="SitePage">
    <File Path="SitePage\WebPartPage.aspx" Url="SitePage/WebPartPage.aspx" >
      <NavBarPage Position="End" ID ="1002" Name="Sample web part" />
      <AllUsersWebPart WebPartOrder="0" WebPartZoneID="Left">
        <![CDATA[
          <webParts>
            <webPart xmlns="http://schemas.microsoft.com/WebPart/v3">
              <metaData>
                <type name="HowToAddWebPart.WebParts.SampleWebPart.SampleWebPart, $SharePoint.Project.AssemblyFullName$" />
                <importErrorMessage>$Resources:core,ImportErrorMessage;</importErrorMessage>
              </metaData>
              <data>
                <properties>
                  <property name="Title" type="string">Sample WebPart - using AlluserWebPart element</property>
                  <property name="Description" type="string">Sample WebPart desctiption here.</property>
                </properties>
              </data>
            </webPart>
        </webParts>
        ]]>
      </AllUsersWebPart>
    </File>
  </Module>

When you use second approach, you must get SPLimitedWebPartManager instance from page and add a web part:

var manager = web.GetLimitedWebPartManager(SPUrlUtility.CombineUrl(web.Url, "SitePage/WebPartPage.aspx"), PersonalizationScope.Shared);
var webPart = new SampleWebPart();
webPart.Title = "Sample WebPart - using Feature receiver";
manager.AddWebPart(webPart, "Left", 1);

As usual, you can download sample here (13.20 kb)

Tags: ,

SharePoint | Web Parts

Web Parts. Web parts connections.

by Kai 20. June 2011 04:06

Hi 2 all!

Next post about web parts is about web parts connections. All my post listed here:

How to create connections between two web parts and provide data? I’ve created simplest ever example just to illustrate how it works. Let’s connect our web parts!

First of all you must create public interface, which will act as data transfer between your web parts. So, let’s create it:

public interface IDataProvider
{
	string Name { get; }
}
More...

Tags: ,

SharePoint | Web Parts

Web Parts. Custom editor part (even more - custom visual editor part) and web parts properties.

by Kai 19. June 2011 19:10

Hi 2 all!
I continue to write posts about using and programming web parts. Here listed all topics about web parts:

In this post I will give a little sample about how to use web parts properties and how to write custom editor part. Let’s start. 

Every web part contain a set of  base properties, you have the opportunity to create custom properties.  Web part property is a object that user can customize. Every property has a set of attributes, which associated with it. This attributes describe property settings, such as name, description, and others. Let’s see the declaration of a simple web part property:

[WebBrowsable(true),
 WebDisplayName("Greetings"),
 WebDescription("user greetings"),
 Category("Custom settings"),
 Personalizable(PersonalizationScope.User)]
public string Greetings
{
	get;
	set;
}

More...

Tags: ,

SharePoint | Web Parts

Web Parts. Web parts verbs.

by Kai 19. June 2011 01:01

Hi 2 all!
This is my next post relative to web parts. Today I’m going to show some samples about how to use web parts verbs. So, the other topics are:

Web part verbs are actions that available through top-right corner of web part. You can see standard actions when click arrow button:

You can develop custom verbs in your web parts. All you need is to override public property Verbs from base WebPart class. This property returns WebPartVerbCollection that contains all verbs for current web part. In property getter you need to create custom WebPartVerb and add it to the collection. WebPartVerb class has three constructors – the first take string ID of verb and string name (or code) of javascript function that will be executed, the second – string ID and WebPartEventHandler that will fire when you click on web part verb, and the last constructor override take both client and server method. Let us see it in action.

More...

Tags: ,

SharePoint | Web Parts

Web Parts. Visual Web Part.

by Kai 17. June 2011 00:12

Hi 2 all!
This is my second post in blog post series about web parts. Other posts you can find here:

In this post I'm going to show how to use visual web part (note: visual web part is not available at sandboxed solutions. Visual web part may be is the most easy to understand and use. To write visual web part we need user control, which must be deployed into controltemplates directory. In a web part code file in CreateChildControls() we call method Page.LoadControl() that takes the string path to user control as a parameter, then simply add loaded control to current control collection.

private Control userControl;
protected override void CreateChildControls()
{
	userControl = Page.LoadControl(userControlPath);
	Controls.Add(userControl);
}
You may load as many controls to your web part as you want. But when you call method Controls.Add() method, that isn’t mean that your control already in a page. Controls adds to a page in RenderContents() method, if you override it, you can manage sequence in which controls will add, or add some additional tags to your web part:

private const string userControlPath = @"~/_CONTROLTEMPLATES/HelloWorldVisualWebPart/HelloWorldVisualWebPartUserControl.ascx";
private const string anotherControlPath = @"~/_CONTROLTEMPLATES/HelloWorldVisualWebPart/AnotherUserControl.ascx";

private Control userControl;
private Control anotherControl;

protected override void CreateChildControls()
{
	userControl = Page.LoadControl(userControlPath);
	anotherControl = Page.LoadControl(anotherControlPath);
	Controls.Add(userControl);
	Controls.Add(anotherControl);
}

protected override void RenderContents(HtmlTextWriter writer)
{
	writer.AddAttribute(HtmlTextWriterAttribute.Id, "wrapper");
	writer.RenderBeginTag(HtmlTextWriterTag.Div);
	anotherControl.RenderControl(writer);
	userControl.RenderControl(writer);
	writer.RenderEndTag();
}
So, as you can see visual web parts are easy to use and develop, in downloads you can find sample project with above code. You can download it here(19.01 kb)

Sorry for my English.

Tags: ,

SharePoint | Web Parts