Chapter 9 – Sharepoint 2010 Web Parts

Wrox Real World Sharepoint 2010 (P.269)

Default CrateChildControls method uses Page.LoadControl to load the declarative user control from the _controltemplates virtual directory. That requires the ASCX file be deployed to the {SharepointROOT}\TEMPLATE\CONTROLTEMPLATES\ProjectName folder.

In ASP.NET, two ways to create application:
(1) Declarative:
<asp:TextBox Text”…”/>

(2) Imperative:
TextBox t=new TextBox();
t.Text=”Default Text”;

XSL List View (XLV) Web Part is used to display most Sharepoint List.

A Sharepoint web application will run a web part only if it is explicitly identified as SafeControl in web.config.
<SafeControl Assembly=”…………” NameSpace=”…..” Safe=”True” AllowRemoteDesigner=”True”…./>

The SafeControl element should always be added by Sharepoint Solution, not by hand.

Web Part Framework —
A minimum of two files make up a web part:
(1) web part definition (an assembly, derived from System.Web.UI.WebControls.WebParts.WebPart)
(2) web part template (*.webpart, and XML file)

Application Pages

are always found in the _layout
virtual directory. (SHAREPOINTROOT\TEMPLATE\LAYOUTS) Deployed using a farm solutions. Cannot be edited, created by end users.
Available to all sites in the farm.

Content Pages
– found in Content Databse. Two flavors (1) Instance pages and (2) Publishing pages.

*** Instance Pages

(system pages in publishing context) are content pages not based on Page Layout. Use the MasterURL property of the SPWeb object to decide which Mater Page to use.
Type of instance pages:
(1) Web Page pages – include web part zones
(2) Wiki Pages – One rich content area
(3) Meeting workspace tab page
(4) ASPX/HTML page – can onlt be created from Sharepoint Designer

examples of instance pages:

*** Publishing Pages
– based on a page layout
and stored in Pages Library. Use CustomMasterURL of SPWeb object to decide which master page to use.

Master Pages —
Master pages in the Master Page Gallery are for content pages
Master pages in _layout are typically for Application pages

Page Layouts —

*** All Publishing Pages must identity which Page Layout they are Using.
Publishing Page = Page Layout

Site Collection Web Part Galleries – (_catalogs/wp)
web part teplates uploaded to the site collection’s web part gallery are available anywhere within the site collection.
If web part template is orphanated for any reason, use this method to “new up” the template:
(1) Site Collection
à Web Part Gallery à Select New Document from the ribbon:

(2) Select a web part.

Web Application Web Part Gallery – (wpcatalog)
Web part templates placed in the physical wpcatalog folder just off the web app’s IIS root will show in all pages of all site collections within the web application.

Web Part Manager – a control that typically placed on master page to manage all moving parts of a web part zone.

Web Part Zone —
two types:
(1) WebPartZones: contains all web parts on a web page
(2) ToolZone: specialized zone that enables users to change the appearsance, behavior, layout and properties of the web aprts.

Web Part Ribbon —
For troubleshooting: add contents=1
t the end of URL and you see all the web parts on this page.

When you create a Sharepoint project in VS 2010, always select Empty Sharepoint Project.

(1) VS 2010, New Project à Empty Sharepoint Project à Name: Mindsharp.Webparts.RealWorld à Sandbox Solution
(2) Right click project root
à Add New Item à Visual Web Part à named FirstPart.
(3) Open the Elements.xml file in the web part folder, and change this property:

(instead of using “Custom”, we use a “RealWorld” as the category name)
(4) The featue will be deployed to {SharepointRoor}\TEMPLATE\FEATURES.
(5) Rename Feature1 to SiteFeature, double click it to show the Feature Designer
(6) Select “Site” (site collection) as scopt.

(7) Make sure Firstpart is in the feature

(8) Select View
à Other Windows à Package Explorer to show the package explorer

In Packaging Explorer, select Validate:

The validation will fail because Visual eb Part cannot be deployed as a sandbox solution. Once changed to Farm solution, validation will succeed:

Open the webpart ASCX file, Design View
à Add a Label, change to Code View, update the code:

****** A module feature is used to copy the web part template file into the Web Part Gallery. However, when a Module Fetaure is deactivated, it orphans the file in Content Database. To end users, it would seem like that they can still add the web part, but rather they get an error message. So when a Module Feature is used, housekeeping code is needed in the FeatureDeactivating method of the feature receiver.

In the *.cs file, add this line above the class:

To generate the GUID, in VS 2010, use Tools à Create GUID. CTRL-U to make sure all are lower case.

Go to Web Part property’s Feature Property, click the “…” button.

Click Add and change the key name to MyAssemblyQualifiedName

Type this in the value field:

Now build solution:

Right click Sitefeature, add Event Receiver:

Copy te following code in the FeatureDeactivating method:

Click the web part folder to set the Deployment Conflict Resolution property:

None – an error is thrown during a conflict
Prompt – display a prompt
Automatic – VS tries to solve the conflict

Deploy the solution:

Verify you can see the web part

Post a comment or leave a trackback: Trackback URL.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: