In my blog post, MSDN Webcast: Creating Microsoft Office 2010 Add-Ins Using SharePoint 2010 as a Data Source, I pointed you to the webcast discussing how to create Office add-ins that leverage SharePoint 2010 data. But here I’m pleased to announce the July, 2010 MSDN article, Office Add-Ins: 3 Solutions for Accessing SharePoint Data in Office 2010, that Paul Stubbs and I co-authored. This article provides prescriptive guidance on how to build add-ins that access SharePoint using three different approaches:
- Using the client object model to access a SharePoint External list (same code as demonstrated in the webcast)
- Using WCF Data Services to access SharePoint standard lists (same code as demonstrated in the webcast)
- Using SharePoint web services
We discuss a few no-code solutions for landing SharePoint data in Office along the way as well.
Sample code for #1 and #2 is available here, http://code.msdn.microsoft.com/201007OfficeAddins.
To help you with setup, if you are using the SharePoint 2010 RTM VHD, you can go here, http://cid-a88aadb6e5827318.office.live.com/browse.aspx/.Public/MSDN%5E_2010%5E_July%5E_Article, and download a MSDN_Article_Setup_Files.zip file. This contains a PowerShell script to setup the SharePoint site on the VM.
Create the site:
- Download/extract the zip on the SharePoint 2010 RTM VM
- Double-click the SetupSPC.bat file. Depending on your CPU and memory this may take a while to complete)
- Once deployed, open the browser and navigate to http://intranet.contoso.com/sites/SPC just to verify.
Now that you have the site setup, to prepare to run the client object model add-in, you first need to create a database view of the CRM data you will be using and then create an External Content type in SharePoint designer and then the corresponding External List. I used the AdventureWorksLT database.
Create a Database view:
- On the SharePoint 2010 RTM VM, Open the SQL Server Management Studio, and click Connect at the prompt.
- Expand the Databases, AdventureWorksLT and Views nodes.
- Right-click on the Views node and select New View…
- In the Add Table dialog, select Address, Customer and Customer Address. Click Add and close the dialog.
- In the designer, select all the SQL statement and delete it. Paste in the following SQL:
SELECT SalesLT.Customer.CustomerID, SalesLT.Customer.FirstName, SalesLT.Customer.LastName, SalesLT.Customer.CompanyName, SalesLT.Address.AddressLine1,
FROM SalesLT.Address INNER JOIN
SalesLT.CustomerAddress ON SalesLT.Address.AddressID = SalesLT.CustomerAddress.AddressID INNER JOIN
SalesLT.Customer ON SalesLT.CustomerAddress.CustomerID = SalesLT.Customer.CustomerID
- After pasting in the SQL, click in the designer window for the tables so the tables refresh according to the SQL statement.
- Click on the Save icon and name the view, vCustomerAddress. You can click the Execute SQL icon to see the resulting data if you like and then close the managment studio
Create the External Content Type (ECT) for the database view:
- Open IE and navigate to http://intranet.contoso.com/sites/SPC. Choose Site Actions, Edit in SharePoint Designer (SPD).
- Once SPD opens, select External Content Types under Site Objects. From the ribbon New group, click External Content Type.
- For the Name and Display name provide: Customers From CRM
- For the Office Item Type choose: Contact
- Click the External System, click the link.
- On the Operation Designer page, click the Add Connection button and choose SQL Server for the data source type. Click Ok.
- In the SQL Server Connection dialog provide:
- Database Server: DEMO2010A
- Database Name: adventureworkslt
- Name: adventureworkslt
- Click OK.
- Expand the adventureworkslt, Views nodes. Right-click on vCustomerAddress. Since you are using a view (read-only) you’ll only create two read operations for this ECT. Select New Read Item Operation.
- Click Next, on the Operation Properties page.
- Click Next on the Input Parameters Configuration
- On the Return Parameter Configuration click on each data source elements, one at a time, and map it to the corresponding Office Property drop-down list Item.
- First Name –> First Name (Firstname)
- Last Name –> Last Name (LastName)
- Company Name –> Company Name (CompanyName)
- AddressLine1 –> Business Address Street (BusinessAddressStreet)
- City –> Business Address City (BusinessAddressCity)
- StateProvince –> Business Address State (BusinessAddressState)
- Click Finish.
- Now you need to do the same for the Read List Operation (an ECT requires a Read Item and Read List Items operation before it can be created). Right-click vCustomerAddress view again and choose New Read List Operation.
- Click Next
- Click Next
- On the Return Parameter Configuration page you don’t need to map these this time, but click on each of the following individually and click the Show In Picker checkbox. FirstName, LastName and CompanyName.
- Click Finish.
- Click the Save icon to save the new ECT.
Create a new External List:
- In SPD, click on External Content Types under the Site Objects and then click on your new Customers From CRM ECT so it is highlighted (do not click on the link itself, you don’t want to edit the ECT).
- Now click the External List button in the New group on the ribbon.
- Provide the name Customers in the dialog box.
- Open IE and see your new Customers External List at http://intranet.contoso.com/sites/SPC.
- Now you’re ready to run the CSOM-Quote solution. Load it up in Visual Studio 2010 and Start Debugging. You’re result should look like this:
To prepare for the WCF Data Services example, you need to deploy the solution that creates the Course and Course Category lists and establishes the relationship between them. You can then make up your own course/course category data for testing.
Create the Course/Course Category lists and load the data:
- In Windows Explorer, navigate to the unzipped CreateCourseList.sln file in the CreateCourseLists folder and double-click it.
- In Visual Studio 2010, right-click on the solution file and select Deploy Solution. Again, this may take a while depending on your configuration.
- Once deployed, open IE and navigate to http://intranet.contoso.com/sites/SPC (this may take some time for IIS to fire up the site from the deployment) and click the Course Category list link.
- Add 3-4 categories for courses, e.g. Database Management, Office Development, or whatever course categories of interest to you.
- Then click the Course list link and create some new courses. The data you put in here can be any values you choose, but you’ll need to select a category for each course. Add a few courses for each of your categories.
- Once the data is loaded you’re ready to run the Conf-DS solution. So load it up in Visual Studio 2010 and select Start Debugging. Your result should look something like the following. Notice that I have applied some styling to Course 3320. This is to show that once the data lands in Word the user has complete control over it and can apply their design skills (clearly mine are lacking) to create the document. Course 2211 simply shows that the data is initially inserted as text.
All in all, this should get you rolling with landing SharePoint 2010 data in Office! Be sure to follow Paul’s blog for the guidance he has on the Outlook add-in that uses the UserProfileService web service.
A special thanks to John Durant for reviewing and adding his touch to the article.