Creating a SharePoint BCS .NET Connectivity Assembly to Crawl RSS Data in Visual Studio 2010
In this post, I’ll walk you though how to create a SharePoint 2010 BCS .NET Connectivity Assembly in Visual Studio 2010.
We’re going to create an assembly that will allow us to Crawl RSS Feeds using BCS.
Creating a BCS .NET Connectivity Assembly project in Visual Studio 2010
First, create a new project selecting the Sharepoint –> 2010 –>Business Data Connectivity Model. I’ll call this project RSSModel.
At the SharePoint Customization Wizard screen, select Deploy as a Farm Solution. Be sure to put in the address of your base web application.
Click Finish. Visual Studio will create the new project. Once complete you should be presented with the model designer surface as below:
Updating the Default Project for Our Custom BCS Model
Setting the Model Name
The model is created using default model and entity names. Lets change those first. In solution explorer, change the name of the model (BdcModel1) to something that describes our project better. Lets change it to RSSItemModel.
Also, change BdcModel1.bdcm to RSSItemModel.bdcm.
Creating Entity Classes
Next, we’ll want to change the default Entity1.cs and Entity1Service.cs. Since we will be returning RSS Item entities here, lets change the Entity1.cs file to RSSItem.cs, and the Entity1Service.cs file to RSSItemService.cs.
(Generally, when you change a class file name, Visual Studio will automatically rename the class. I’ve noticed in my environment this has not happened 100% of the time. Also, changing the BDCModel folder name does not automatically change the namespaces on the class files below it. Open the RSSItem and RSSItemSErvice files and update the class names and namespace names as seen below)
Next, lets update our Entity class. We’re interested in indexing RSS Item entities here. I’m going to use the Google News feed available at http://news.google.com/news?pz=1&cf=all&ned=us&hl=en&output=rss
Below is a snippet of XML from that feed:
Using the fields found in the RSS Snippet above, we’ll construct the RSSItem class as below
Next, lets update the RSSItemService class. The sample class created for us had 2 methods, ReadItem() and ReadList(). These are the two methods needed in an External Content Type in order to consume information in SharePoint. Since we’re not interested in Insert, Update, or Delete operations, we’ll just modify the two methods here to return information for RSS Items in the feed.
Note: Shortcuts were taken here in the interest readability and post size. Ensure all production code uses proper exception handling and null checking.
Updating the Model in the Designer
Next we’ll update the RSSItemModel in the designer. First, open the designer surface by double-clicking RssItemModel.bdcm in solution explorer.
Click somewhere in the background of the designer surface. If the Name property in the Properties pane still says BdcModel1, be sure it gets changed to RSSItemModel:
Next, select the “Entity1” entity on the designer surface. In the Properties window, change the Name property to RSSItem and the Namespace property to RSSModel.RSSItem.
Next, click Identifier1 under Indentifiers in the RSSItem entity on the model surface. Change the name to Guid, which is the identifier for our External Content Type. Ensure the Type Name property reflects the .NET type of the field we’re using for the identifier (in this case, System.String).
Next, we’ll edit the BDC Methods. First, click the BDC Method Details tab at the bottom of the editor windows.
Expand the ReadList node. Click Entity1List under the Type Descriptor column. Click the drop down arrow and select <edit>. In the Properties Box, change the Name property to RSSItemList
Expand the Instances nodes under ReadList. Select the ReadList node. In the Properties box, change the Default Display Name property to RSSItemList.
Next expand the ReadItem node. Click Identifier1 in the Type Descriptor column. Click the drop down arrow and select <Edit>. In the Properties window, change the Name property to the name of your identifier field (in our case, Guid).
Next, click Entity1 in the Type Descriptor column. Click the drop-down arrow and select <Edit>. Change the Name property in the Properties window to RSSItem.
Expand the Instances node under the ReadItem node. Click the ReadItem node under Instances. In the Properties window, change the Default Display Name property to RSSItem.
Next, click the BDC Explorer tab, which should be next to the Solution Explorer tab.
Updating Type Descriptors for Entities in our BDC Method Instances
First, change the name of the LobSystem note in the tree, directly under the RSSItemModel node, currently named BdcModel1. Change it to RSSItemModel.
Next, expand the RSSItem node, and expand the ReadItem and returnParameter nodes beneath it. Expand the RSSItem node. You should see something similar to the following:
First, click the Identifier field. in the Properties window, be sure the Identifier property reads Guid. If not, set the Identifier property to Guid. Next, change the Name property to Guid and ensure the Type Name property is set to System.String.
Next, select the Message node under RSSItem in the tree. Change the Name property to match the next property in our entity (in our case, Title). Ensure the Type Name property matches the .NET Type of that property in our entity (in our case, System.String).
Now, we need to add Type Descriptors for the remaining columns in our entity. For each remaining property in our entity, right click the RSSItem node under returnParameter in the BDC Explorer tool window and click Add Type Descriptor.
Fill in the Name and Type Name properties for each remaining property using the values in the table below:
You should end up with an RSSItem tree resembling the following:
Next, expand the ReadList, returnParameter, and RSSItemList nodes. Select the Entity1 node under RSSItemList. Right click the node and click Delete.
Next, select the RSSItem node under ReadItem –>returnParameter that we just edited above. Right click the node and click Copy.
Next, right click the RSSItem node under ReadList –> returnParameter and click Paste.
This should result in a ReadList node that resembles the screenshot below.
Next, build the solution.
Note: If you encounter build errors, check your RSSItemService.cs to be sure stubs for BCS model operations have not been added by the designer to the bottom of your class. If they have, delete them, leaving the ReadItem and ReadList methods we changed earlier.
Setting Required BCS Custom Properties
Site URL Property
We have to set a BCS Custom Property to allow us to successfully deploy the BCS model. For some reason, the SharePoint Configuration Wizard sets the Site URL project property, but doesn’t create the SiteUrl property in our Feature that is needed for successful deployment.
To add this property, click RSSItemModel in Solution Explorer. in the Properties window, click on (Collection) next to Feature Properties and click the ellipsis (…) button.
In the Feature Properties dialog, click Add. Set the Key property to SiteUrl and the Value property to the URL you selected in the SharePoint Configuration Wizard when you created the project. (Note: this should be the same value as the Site URL property in the project properties)
Setting BCS Custom Properties to Make BCS Model Searchable In SharePoint 2010 Search Service Applications
Just as with the SiteUrl BCS Custom Property, there are other custom properties we need to set in order to make the External Content Type we’ve created usable in SharePoint Search as a Line of Business content source.
First, we need to set the ShowInSearchUI property on the LobSystemInstance to make the external content type show up in the Content Sources properties in the SharePoint Search SSA (and/or FAST Search Content SSA)
Open the model design surface by double-clicking RSSItemModel.bdcm in the Solution Explorer tool window. Click the BDC Explorer tab (next to Solution Explorer), and expand the Model, RSSItemModel, RSSItemModel, and LobSystemInstances nodes. Click the node under LobSystemInstance, which should be named RSSItemModel. If it still reads BdcModel1, not, rename it to RSSItemModel.
Click (Collection) next to Custom Properties in the Properties tool window and click the elipsis (…) button.
In the Property Editor window, type ShowInSearchUI in the Name column and hit the enter key. Double-click System.String in the Type column, and select System.Boolean from the drop down box. Double-click the empty Value column and enter true in the box. The result should look like the screenshot below. Click the OK button when done.
Next, we need to identify the RootFinder method instance. The RootFinder method instance allows SharePoint to know which method to use to enumerate the entity collection for search.
Click the RSSItem entity in the designer pane and select the BDC Method Details tab in the bottom tool window.
Under the Methods node, click the ReadList node. Click (Collection) next to Custom Properties in the Properties tool window and click the elipsis (…) button.
In the Property Editor box, set the Name column to RootFinder, type to System.String, and Value to x. Click OK when done.
Next, expand the Instances node under Methods –> ReadList in the BDC Method Details tool window. Click ReadList. Click (Collection) next to Custom Properties in the Properties tool window and click the elipsis (…) button.
In the Property Editor box, set the Name column to RootFinder, type to System.String, and Value to x. Click OK when done.
Setting the Title Property for our BCS Model
Next, we’ll set the Title field for the entity. If this BCS Custom Property is not set, any search results that are returned from this content source will have the name of the BCS Profile Page as the title, and they will all be identical. This makes the search results less valid.
In the BDC Explorer tool window, click RSSItem under Model –> RSSItemModel –> RSSItemModel. Click (Collection) next to Custom Properties in the Properties tool window and click the elipsis (…) button.
In the Property Editor window, add a new property. In the Name column, enter Title. In the Type column, set System.String. In the Value column, we will put the name of the property on our entity that we want to use as a title for search results. In this case, it’s Title. Click OK when done.
Now, build and deploy the project.
Verifying Successful Deployment of the .NET Connectivity Assembly
Configuring the External Content Type in the BCS Service Application
Open up Central Administration on your SharePoint 2010 instance, and navigate to your BCS Service Application Instance. (Central Admin –> Manage Service Applications). If the .NET Connectivity Assembly has been successfully deployed, you will see the RSSItem entity listed in the table as below:
Click the checkbox next to the RSSItem entity in the table, and click the Configure button on the toolbar in the Profile Pages section.
In the Configure External Content Type Profile Page Host select the SharePoint site where you want your ECT Profile Pages created. Click OK at the bottom of the popup window when done.
Check the checkbox next to RSSItem in the table and click Create/Upgrade in the Profile Pages section of the toolbar.
A Loading… popup window will appear while the profile pages are created. When completed, an informational dialog will appear. Click OK.
Another window will be displayed when the profile page has been successfully created.
Next, check the checkbox next to RSSItem again, and click Set Object Permissions in the Permissions section of the toolbar.
In the Set Object Permissions box, add appropriate permissions for the search service account, content access account, and any app pool accounts for any web applications that will be using, consuming, or displaying data from the RSSItem External Content Type.
Click OK when done.
Setting up a Content Source for Crawling our BCS Entity Data
In Central Administration, navigate to your Search SSA. This will either be your SharePoint Search SSA if using SharePoint Server Search, or your FAST Content SSA if using FAST.
Click Content Sources under crawling. In Manage Content Sources click New Content Source.
In the Name field, type a name for this content source. I’ll enter RSS. For Content Source Type select Line of Business Data. This should change the rest of the page to show external data sources. Under External Data Source, you should now see an enabled drop-down labeled Select the Business Data Connectivity Service Application, and two radio buttons, one entitled Crawl all external data sources in this Business Data Connectivity Service Application, and one entitled Crawl selected external data source.
Select the second radio button, and check RSSItemModel.
Under Start Full Crawl, check Start full crawl of this content source, and click OK.
Let the crawl start up and complete. You can click the Refresh button to monitor the crawl, or look at the Crawl History table on the Search Administration page.
Note: If your crawl doesn’t execute successfully, look at the crawl log for error messages. (messages sometimes take a few minutes to appear in the log after the crawl completes) I had unsuccessful crawls due to an account I missed putting in the BCS Set Object Permissions dialog.
Validating Search Results
Once the crawl is complete, go to your Search Center page and execute a search for a term you expect to see in the RSS Feed. I believe there was an article about the president in the top feeds, so I’ll search Obama in my search center.
As you can see, we get 2 results, and the Title of the search result is the value from the Title element in the RSS feed, which we set using the Custom BCS Property.
That’s it. Please leave any questions in the comments section!