| |
| Exporting a SharePoint
List to Word with SoftArtisans OfficeWriter |
 |
| Goal: |
| SharePoint “Features” make
it easy to extend your site’s functionality. This walkthrough
will demonstrate how a custom feature can be used to bridge SharePoint
and Office. We will create a Feature that uses SoftArtisans OfficeWriter
to export a SharePoint List to a Word document through a new item
in the “Actions” menu.
We will use a solution package so that our Feature is easily deployable.
Microsoft Windows SharePoint Services 3.0 introduces a deployment
mechanism named "solution packages." A solution package
is a CAB file with a .wsp file-name extension that contains all
the files that must be deployed on the front-end Web server and
a set of XML-based installation instructions.
|
| |
| |
 |
Setting
up the WSP project |
| |
| 1. |
Launch VS.NET and create a new Class Library
project. |
| |
|
| 2. |
Create a set of folders and subfolders under the project’s
root that correspond to folder structure where the feature will be
deployed on the SharePoint Server. This is typically the “C:\Program
Files\Common Files\Microsoft Shared\web server extensions\12”
folder. |
| |
| You will need to create the following
folders: |
 |
Solution |
 |
Template |
 |
Template/Features |
 |
Template/Features/OfficeWriterWord_ExportList |
 |
Template/Images |
 |
Template/http://lib.store.yahoo.net/lib/softartisans/Tpg |
 |
Layouts |
 |
Layouts/OfficeWriterWord_ExportList |
|
| |
| |
| NOTE:
You will need to create a new GUID for the “Id”
nodes in the “Feature.xml”, “Elements.xml”
and the “Manifest.xml” files. GUIDs can be generated with
guidgen.exe or at http://www.guidgen.com |
| |
|
|
3.
Add a new “Feature.xml” in the project’s TEMPLATE/FEATURES/OfficeWriterWord_ExportList
folder.
The structure of the xml is as follows: |
 |
| |
|
|
4.
Add a new “Elements.xml” to the same folder. The most
important node in this file is the “UrlAction”. This is
the page that SharePoint will hit when the end user clicks on our
menu item. We need to send the UrlAction page the id of the List that
we are exporting. Note the query string with the “ListId”
variable at the end of the url. Windows SharePoint Services makes
this token available in order to identify the List that initiated
the event. For more information see How
to:Add Actions to the User Interface.
The structure of this xml is as follows: |
 |
| |
|
|
| 5.
If you want to associate an image with the menu item, place it in
the TEMPLATE/http://lib.store.yahoo.net/lib/softartisans/TPG folder. |
 |
For this project, were using the file “wordicon.gif.” |
|
| |
|
|
6. Create
a Solution Manifest file, “Manifest.xml”, that gets added
in the SOLUTION folder.
This file specifies the structure of the solution(WSP) file. |
 |
NOTE: It is important
to create a new GUID for the “Feature.xml”, “Elements.xml”
and the “Manifest.xml” files.
GUIDs can be generated with guidgen.exe or at http://www.guidgen.com. |
| |
|
|
| |
|
|
| 7. Next,
create the DDF file in the SOLUTION directory. This file, “OfficeWriterWord_ExportList.ddf”,
specifies the order of files within the WSP file. |
 |
| |
|
|
| Be sure that the
DiskDirectory1 value is ”Solution” in order to ensure
that the WSP will be created in the SOLUTION folder of the project. |
| |
|
|
| 8.
Remove the default “class1.cs” file from the project as
it will not be used here. |
| |
|
|
| 9.
Implement the ActionUrl page. The “UrlAction” page is
where the good stuff happens. It is the target of the “UrlAction”
from the Elements.xml file. Be sure this file is in the directory
TEMPLATE/LAYOUTS/ OfficeWriterWord_ExportList. |
| |
|
|
| |
|
|
| The
Code: |
|
|
| 1. |
Since a user will not
be interacting with this page directly, we do not need to handle
the type ASP.NET events. We simply write the C# to create a
Word document from the SharePoint List. |
| 2. |
We will be using the SharePoint
API to get the contents of the list, so be sure to reference
the SharePoint assembly: <%@ Import Namespace="Microsoft.SharePoint"
%> |
| 3. |
We will be creating the Word document
using OfficeWriter, so we need to reference the OfficeWriter
assembly: <%@ Import Namespace="SoftArtisans.OfficeWriter.WordWriter"
%> |
| 4. |
We’re writing the contents
of a SharePoint list to the Word document, so first we parse
the List id from the query string we defined in the “UrlAction”
node of Elements.xml:
string listId = Page.Request.QueryString["list"]; |
| 5. |
Next we get the SPList reference
that represents the List we want to export
SPWeb site = SPContext.Current.Web;
SPList list = site.Lists[new Guid(listId)]; |
| 6. |
We then create an OfficeWriter Word
document reference by calling WordApplication.Create().
This method creates a new Word document.
WordApplication wap = new WordApplication();
Document doc = wap.Create(); |
| 7. |
Then we create a Table in the document.
TableFormatting oTableFormatting = doc.CreateTableFormatting();
SoftArtisans.OfficeWriter.WordWriter.Table tbl = doc.InsertTableAfter(collListItems.Count+1,
3, oTableFormatting); |
| 8. |
We then iterate over the items in
the SPList and create an new CharacterRun of text for each list
item
SPListItemCollection collListItems = list.Items;
int row = 1;
foreach (SPListItem listItem in collListItems)
{
tbl[row,0].InsertTextAfter(listItem["Salesperson"].ToString(),
true);
tbl[row,1].InsertTextAfter(string.Format("{0:$#,#.00}",
listItem["Year-to-Date Sales"]), true);
tbl[row,2].InsertTextAfter(string.Format("{0:$#,#.00}",
listItem["Previous Year Sales"]), true);
row += 1; |
| 9. |
9. Finally, we send the new Word
document to the client using the HttpResponse object of the
current Page
wap.Save(doc, Page.Response, "OfficeWriter_SPListExport.doc",
false); |
| |
|
| |
The actual code to create a richly
formatted Word document is a little more complex. However, the
details of the OfficeWriter API are beyond the scope of this
walkthrough. Please see the comments in the sample code and
the OfficeWriter documentation for a more detailed explanation. |
|
| |
|
|
| |
|
|
| Requirements:
|
|
|
It is important
to have an active installation of the OfficeWriter “Enterprise
Edition” (EE) evaluation or licensed version on the server where
this code executes. OfficeWriter can be secured by visiting the “Evaluation
Software” page available on the SoftArtisans website at http://support.softartisans.com/Login.aspx?SiteReturnUrl=eval.
Be sure to request the EE version license key.
The OfficeWriter assembly: “SAWW3NET.dll” must be installed
in the GAC on this server since it’s referenced with the import
statement at the top of the .aspx page, along with the SharePoint
assembly.
Modify the “web.config” for the targeted SharePoint site
collection to include the OfficeWriter assembly, in addition to the
already present SharePoint assembly reference. <assemblies>
<add assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral,
PublicKeyToken=71e9bce111e9429c" /> <add assembly="SAWW3NET,
Version=3.8.1.483, Culture=neutral, PublicKeyToken=f593502af6ee46ae"
/> </assemblies>
Make certain that the version property matches the one installed in
the GAC. |
| |
|
|
| |
|
|
| Building
and Deploying the WSP to the SharePoint Server: |
| In order to simplify
the building and deployment process, consider installing the “SPDevMod”
project on your development workstation. It was created by SharePoint
MVP, Scot Hillier and is available for downloading at http://www.codeplex.com/SPDevMod.
A full set of instructions is included. Scot also offers a comprehensive
blog entry entitled: “What's Your Process for Developing SharePoint
Features and Solutions?” at http://scothillier.spaces.live.com/blog/cns!8F5DEA8AEA9E6FBB!197
entry which provides a step-by-step and insights on the entire build/deploy/modify/redeploy
process. Follow the steps outlined there to build and deploy using
the features added with “SPDevMod”. |
| |
|
|
| |
|
|
| Enabling
the Feature: |
|
|
| After deploying
the newly created WSP file to the SharePoint server, go to the targeted
site and click on the “Site Actions” menu and select the
“Site Settings” drop list option. Click on the “site
collection features” link in the “Site Collection Administration”
section. The new “OfficeWriter Word Export Option” feature
should be in the list. Click the “Activate” button to
add this option to the “Actions” menu for your lists. |
| |
|
|
| |
|
|
| Testing
the Feature: |
|
|
| 1.
Go to any List in your SharePoint installation. |
| |
| 2.
Click on the “Actions” menu. You should see the new menu
item, “Export this list to Word with OfficeWriter.”, |
 |
| |
|
|
3. When
you select the new menu item, the code in the “UrlAction”
page will run and the Word document will be sent to the browser.
The following dialog box should appear: |
 |
| |
| 4. Click
“Open” to open the document in Office. |
 |
| |