|
Using the Web Part
Templates Part 2:
Deploying Web Parts
/ Utilisation de Modèles
de Web Part et
déploiement.
TRADUCTION A FAIRE :
Description:
As many people have
discovered recently,
deploying Web parts is a
difficult and time-consuming
process. Minor typos in the
web.config or the Web part
definition (DWP) file could
cause the infamous, “Web
Part could not be displayed.
It is not registered as a
safe control,” error
message. Conveniently,
Microsoft has included a
utility with SharePoint that
makes deploying Web parts
manually much like pulling
teeth. The current methods
for deploying Web parts are
not fun at all. I remember
my first couple of Web parts
took several hours to
deploy, because I was
running into so many
problems with the web.cofig.
With the information I am
going to provide in this
article, all of those
deployment pains are in the
past.
For this deployment method,
I recommend having the Web
Part Templates installed as
they do a significant amount
of heavy lifting in this
process and make life much
easier, but they are not
required. However, the
examples in this article
will be based on the Web
Part Templates. For an
introduction to the Web Part
Templates, please see the
first article in this
series.
In the first article, we
discussed how you can make a
Web part with the Web Part
Templates. Now, I am going
to walk you through creating
a new Web part in Visual
Studio and deploying it on a
SharePoint Server.
Deploying Web Parts on
SharePoint is done through
the use of Web Part Packs.
Web Part Packs are just CAB
files with a specific
structure. You will use the
stsadm.exe tool, included
with SharePoint, to deploy
the Web part to the
SharePoint database. The Web
Part Pack Cab file contains
the Web Part Definition
(DWP) file, the assembly you
want to deploy, as well as a
Manifest.xml file, which is
used to tell stsadm.exe how
to deploy the Web part pack.
Luckily, all these files are
created for us automatically
with the Web Part Template,
and all we have to do is
tweak them a little.
In Visual Studio, create a
new Web part library
project. I named mine
DeployWebPart. After that
project is loaded, we need
to add a new project to this
solution for the deployment.
In Solution Explorer, right
click on the solution, and
choose Add? New Project.
Select Setup and Deployment
Projects, and Choose Cab
Project. I named mine
DeployCab.

Now that the project is set
up, we need to add content
to the deployment project.
To do this, right click on
the project and choose Add
-> Project output. Select
Primary Output.

This will add the built
assembly to the cab file. In
addition, you will need to
right click on the project
in solution explorer once
more and choose Add ->
Project Output. This time,
choose Content Files.

This will add the
Manifest.xml and the
WebPart1.dwp to the cab. The
deployment cabinet will be
built for you automatically
the next time you build your
project, but we aren’t done
yet. If you build now, your
Web Part Package will not
work. We need to edit the
Manifest.xml file. When the
project is generated by the
Web Part Template, the
Manifest.xml is created for
you with most of the
information about your
project. If you happen to
change the name of the Web
Part or assembly after
creating the project, you
will need to edit that
information in the
Manifest.xml file. Since we
are using the defaults, we
do not need to modify that
information. However, we
will need to edit the
ClassResources section of
the file. Because we are not
including additional
resources, such as JPG or
GIF files, with our project,
we do not need this section.
You will want to remove:
From your Manifest.xml, if
you were going to include
additional resource files,
such as images, you would
edit or add the FileName
lines. For the majority of
projects, you will not need
this section. Now we are
ready to build our project,
so click Build -> Build
Solution.
With our Web Part Pack built
and ready to go, we just
need to deploy it. To
deploy, you will use the
included stsadm.exe tool,
which is in the \program
files\common files\microsoft
shared\web server
extensions\60\bin directory.
To add a Web pack, you will
use the “–o addwppack”
switch and the “-filename
”switch.

(Note: I copied the
DeployCab.cab to the root of
C to have an easier to read
command. You will find the
file in the output directory
of your project.)
The command copies the Web
Part Pack into the SQL
database and makes it
available to your SharePoint
sites under the Virtual
Server Web Part library. You
can additionally set the
–url switch to deploy the
Web Part Pack to a specific
SharePoint Site rather than
the entire Virtual Server.
If you open your SharePoint
site and choose Modify Page
-> Add Web Part -> Browse,
click on Virtual Server
Library, and you will see
your Web part listed. Select
your Web part and click Add.

When working with Web Part
Packages, stsadm.exe also
provides facilities to
enumerate and remove Web
part packages. To enumerate
which Web part packs are
currently installed on the
server, use the –o
enumwppacks switch. To
remove a Web part from the
SharePoint SQL database, use
the –o deletewppack –name .

Through this process, you
can deploy and test Web
parts significantly easier
than with the previous
method of editing the
web.config and DWP files
manually. Instead, the Web
Part templates take care of
creating the DWP file for
you and the Web Part Pack
deployment takes care of
updating the web.config.
|