Now that you have seen how to fetch and display data from database tables using Service Data Objects (SDOs), let's concentrate on integrating with other data sources. In this part, you will enable the portlet application to fetch data from a Microsoft SharePoint server. SharePoint exposes different web services for working with a lot of its features. Here is a list of web services exposed by SharePoint (see the Resources section here for a link). By using these web services, you can generate skeleton code in IBM® Rational® Application Developer.
Before starting with this section, please see the Setup
section in Part 1.
Enable portlets to fetch SharePoint data
In this section, you will enable portlets to fetch data from SharePoint server.
Important:
Before starting with this
section, be sure to review the Prerequisite and the Setup
sections in Part 1 (see "More content in this series").
Here is a brief summary of things that you will do to use data from SharePoint:
- Generate skeleton code in Rational Application Developer by using SharePoint exposed web services with the Java API for XML web Services (JAX-WS) tool in Rational Application Developer.
- Write business logic on top of the skeleton code to appropriately use data from SharePoint.
- Use the business logic to fetch and show data from SharePoint document libraries. The data fetched will be used and rendered by the portlet in a portal page.
Generate skeleton code from SharePoint web services
To generate skeleton code from SharePoint web services, follow these steps:
- Create a new Java project in the workspace, and name it
SharePointProject, as shown in Figure 1.
Figure 1. Java project
- Select Windows > Show View > Other > Services to open the Services view shown in Figure 2.
Figure 2. Services view
- Right-click on JAX-WS under the Services tab, and select the Create Client option (Figure 3) to open the Web Service Client wizard shown in Figure 4.
Figure 3. Services tab
Figure 4. Web Service Client wizard
- Under the Configuration section in the Web Service Client
wizard, for Server Runtime, click the WebSphere
Application Server v8.0 link to launch the
Client Environment Configuration window so that you can
choose IBM® WebSphere® Portal v7.0 Server,
instead, as shown in Figure
5.
Note:
WebSphere Application Server v8.0, in Figure 5, would be available only if you would have chosen to install it as one of the run-time servers during Rational Application Developer installation.
Figure 5. Client Environment Configuration window
- Similarly, change the client project to SharePointProject, which was created in step 1, so that the Web Service Client window looks like Figure 6.
Figure 6. Web Services dialog window
- Click the Browse button to launch the Select Service Implementation window that Figure 7 shows.
- Enter the URL of the Lists web services
provided by SharePoint and, if necessary, expand to the
third level, as shown in Figure
7.
Note:
You will also be prompted to enter the SharePoint-specific username and password the first time that you connect to a particular web service.
Figure 7. Service Implementation window
- Click OK to return to the Web Service Client wizard.
- Click Next.
- Click Finish.
After clicking Finish, notice that the skeleton code is generated in the src folder of SharePointProject project, as shown in Figure 8.
Figure 8. Generated skeleton code files
- For this tutorial, use the ListsSoap.java interface that was created by the JAX-WS tool from the Lists web service.
Write custom code for SharePoint
Now that you have generated the skeletal code, it's time to write the business logic on top of the skeletal code. The whole code for the achieving this is also packaged in the sample with this tutorial. The code has the logic to fetch the contents from a SharePoint document or picture library that you created in the setup section. In this section let's take a look upon the relevant code snippets.
Authenticate through the SharePoint server
Before you can fetch the contents from a relevant document or picture library, you'll need to authenticate with the SharePoint server. This is achieved through the code for your business logic. This code would make use of the skeletal code to connect and authenticate through SharePoint server. Listing 1 shows how to authenticate with the SharePoint server.
Listing 1. Authenticating with the SharePoint server
public static ListsSoap authenticateForList(String spUserId,
String spPassword, String spWebServiceUrl) {
ListsSoap listSoapPort = null;
Map<String,String> authenticationParameters = new
HashMap<String,String>();
authenticationParameters.put(userId, spUserId);
authenticationParameters.put(password, spPassword);
authenticationParameters.put(webServiceUrl,spWebServiceUrl);
if (spUserId != null && spPassword != null &&
spWebServiceUrl != null) {
Lists listService = new Lists();
listSoapPort = listService.getListsSoap12();
Map<String,Object> requestContext =
((BindingProvider)listSoapPort).getRequestContext();
requestContext.put(webserviceUrl, spWebServiceUrl);
requestContext.put(webServiceUserName, spUserId);
requestContext.put(webServiceUserPassword, spPassword);
}
return listSoapPort;
}
|
In this code listing, using the SOAP port for the lists service, the context of the SharePoint server is obtained and appropriate parameters such as the web service to be invoked (in this case, Lists web service), username, and password of the SharePoint site is passed on to the context. This is enough to authenticate through the SharePoint server.
Note:
As indicated in the setup section in
Part 1, this tutorial is based on the assumption that your
authentication type is set to BASIC for the IIS server on which
the SharePoint server is installed.
Retrieve contents from a document or picture library
Now that you have authenticated through the SharePoint server, the next step is to connect and retrieve contents from a library. Listing 2 shows how to do that. In this example, you will retrieve only the contents of a document library. The contents of a picture library can be retrieved in a similar manner.
Listing 2. Connect and retrieve contents from the SharePoint library
GetListItemsResponse.GetListItemsResult resultantList =
listSoapPort.getListItems(listToBeRetreived, view, CAMLQuery,
fieldsToBeViewed, noOfRows, options, web_id);
|
In the code in Listing 2, the getListItems method queries the SharePoint server for a document or a picture library. The listToBeRetreived parameter specifies the name of the document or the picture library, the contents of which you want to retrieve. Some of the other parameters include a CAMLQuery which is equivalent of a SQL query, as well as the fields or columns that you want to retrieve from the library.
After you have the result of the method call stored in the resultantList variable, you can use the code in Listing 3 to get an instance of the library as an object. Then you can use that object to retrieve the desired content.
Listing 3. Code to get an instance of the SharePoint library as an object
Object resultObj = resultantList.getContent().get(0);
if ((resultObj != null) &&
(resultObj instanceof ElementNSImpl))
{
ElementNSImpl node = (ElementNSImpl) resultObj;
NodeList list = node.getElementsByTagName(row);
MyBean myBean = null;
int i = 0;
while(i < list.getLength()){
NamedNodeMap attributes = list.item(i).getAttributes();
String id = attributes.getNamedItem("ows_ID").getNodeValue();
String name = attributes.getNamedItem
("ows_NameOrTitle").getNodeValue();
myBean = new MyBean();
myBean.setId(id);
myBean.setName(name);
...........................................................
...........................................................
...........................................................
i++
}// end of while
}//end of if
|
In the code in Listing 3, the resultObj object represents an instance of a row in a document or a picture library in SharePoint. As you can see in the Listing 3, the id and name variables contain the ID and title of a row present in a SharePoint library.
The full code for Listing 1, 2, and 3 is inside the SharePointIntegrationManager class within the SharePointProject in the attached sample (see Downloads). You can simply copy the contents of these classes into your own custom classes.
Write a utility class to fetch a URL for a document
You can use the code in Listing 4 to fetch the URL pointing to a document in a document library.
Listing 4. Fetch the URL that points to the document in the library
public String getUrl(String companyName){
List<MyBean> myList=null;
try {
ListsSoap listSoapPort = (ListsSoap)
SharePointIntegrationManager.authenticateForList
(this.userName, this.password, this.spwebServiceUrl);
myList = SharePointIntegrationManager.sharePointList
(listSoapPort, companyName, null, this.rowLimit);
} catch (Exception e) {
}
MyBean bean = myList.get(0);
String docUrl = bean.getFileRef();
docUrl = docUrl.replace(" ", "%20");
if(docUrl!=null){
return docUrl;
}
return null;
}
|
Note:
The
getUrl and
getPicUrl methods fetch a document
and a picture URL from the respective libraries.
The full code for Listing 4 is inside of the SharePointDataAccess class within the SharePointProject in the sample project (see the Downloads section). Again, you can simply copy the contents of this class into your own custom class.
Retrieve content from SharePoint libraries
This section concentrates on enabling and writing code that uses
SharePoint library content retrieval code, as discussed in the
earlier section. You will also be changing the portlet
JavaServer Page (JSP) file so that appropriate SharePoint
library content is displayed.
Using the business logic of SharePoint library content retrieval
The first step in using the APIs that retrieve the contents from SharePoint libraries is to associate the project that contains the API with a portlet project. In this case, associate the SharePointProject project with the DealsProject portlet project:
- In the Enterprise Explorer view, right-click the DealsProject and, from the drop-down context menu, click the Properties option.
- In the Properties window (Figure 9), choose the Deployment Assembly option.
Figure 9. Web Deployment Assembly view
- Click the Add button to launch New Assembly Directive window.
- Choose the Project option, and click Next.
- Choose SharePointProject as shown in Figure
10, and click Finish.
Figure 10. Choosing the deployment assembly project
- Click OK in the main window.
Now that you have associated the SharePointProject with the portlet project, you will write some code to use the API's provided by SharePointProject:
- In the DealsProject, open the SDOPropertResolver class.
- Go to the getValue(Object base, Object property) method, and modify it as shown in the Listing 5.
Listing 5. Method modifications
public Object getValue(Object base, Object property)
throws PropertyNotFoundException {
try {
if (base instanceof DataObject) {
if (property != null && !(property.toString().
equalsIgnoreCase("DOCURL"))) {
//First try custom getter...
try {
return invokeCustomGetter(base, property);
}
catch (PropertyNotFoundException pne) {
//otherwise try the DataObejct getter
return (((DataObject) base).get(property.toString
()));
}
}else{
String companyDocURL=null;
if(property.toString().equalsIgnoreCase("DOCURL")){
DataObject account=(DataObject)(((DataObject) base).get
("DEALS_ACCOUNT_DETAILS"));
String companyName=account.get("NAME").toString();
SharePointDataAccess spDataAccess=
SharePointDataAccess.getInstance();
companyDocURL=spDataAccess.getUrl(companyName);
}
return companyDocURL;
}
}
} catch (NoClassDefFoundError ncdfe) {
// wdo jars are not available
}
if (oldPropResolver != null) {
return oldPropResolver.getValue(base, property);
}
throw new PropertyNotFoundException();
}
|
In this method, whenever there is a property by the name of
DOCURL, the getUrl method of the
SharePointAccess class is invoked. This basically retrieves a
URL of a document hosted in a SharePoint document library. The
input to this method is the name of the actual document library,
which for this example is Liquid Sugar Corp
(recall that you created a document library named Liquid Sugar
Corp in the setup process in Part 1).
Display SharePoint content
Now that you have all of the code for fetching content from SharePoint libraries, you will display it along with the content fetched earlier from the IBM® DB2® database. Make some changes in the portlet JSP, DealsView.jsp:
- Open the DealsView.jsp file in the Page Designer.
- Select the data table that you created earlier, in Part 2 of this series.
- Open the Properties view.
- In the Properties view, click the Add button for columns, as shown in Figure 11.
Figure 11. Properties view
- Enter
Commentsas the column name, as shown in Figure 12.
Figure 12. Add a column in the Properties view
- Save the file.
- Open the Palette view, and expand Enhanced Faces Component drawer.
- Choose Link from the Enhanced Faces Component drawer, as shown in Figure 13, and drag it over the Comments column to invoke the Configure URL window.
Figure 13. Palette
- Enter this in the URL
field:
#{vardealsInformationList.DOCURL} - Enter this in the in the Label field (see Figure
14):
#{vardealsInformationList.DEALS_ACCOUNT_DETAILS.NAME}
Figure 14. Configure URL section
- Save the file.
- Run the portlet project on the server as you did before.
You will see the portlet rendered in browser, as shown in Figure 14. Notice the Comments column. It provides a URL to the respective document stored in a SharePoint document library. If you click on the URL, opens the respective documents.
Figure 15. Portlet with the SharePoint data rendered in a web browser
This concludes the SharePoint integration instructions. You can fetch data from a picture library in a similar fashion. You can also implement the SharePoint data use case for the smart phone JSPs as you did for the desktop portlet JSP in Part 1.
Part 3 shows how to create event-enabled portlets that can share data.
| Description | Name | Size | Download method |
|---|---|---|---|
| Scenarios and use cases for the series | scenarios_use_cases_sample.zip | 5MB | HTTP |
Information about download methods
Learn
- Find out more about Rational
Application Developer:
- Browse the Rational Application Developer for WebSphere Software page on developerWorks for links to technical articles and many related resources.
- Explore the Information Center for detailed instructions, especially the section titled "Creating portlets and portlet projects" (Developing > Developing portal and portlet applications > Portlet development overview > Developing portlets)
- List of web services provided by Microsoft
SharePoint.
- Visit the Rational
software area on developerWorks for technical resources
and best practices for Rational Software Delivery Platform
products.
- Stay current with developerWorks technical events and webcasts focused on a variety of IBM products and IT industry topics.
- Attend a free developerWorks Live! briefing to get up-to-speed quickly on IBM products and tools, as well as IT industry trends.
- Watch developerWorks on-demand demos, ranging from product installation and setup demos for beginners to advanced functionality for experienced developers.
- Improve your skills. Check the
Rational training and certification catalog, which
includes many types of courses on a wide range of topics. You
can take some of them anywhere, any time, and many of the
"Getting Started" ones are free.
Get products and technologies
- Try Rational Application Developer for WebSphere Software,
free.
- Evaluate
IBM software in the way that suits you best: Download it
for a trial, try it online, use it in a cloud environment, or
spend a few hours in the SOA Sandbox learning how to implement service-oriented
architecture efficiently.
Discuss
- Check Rational Application Developer wiki to keep up with
news and to contribute.
- Join the Development Tools forum to ask questions and
participate in discussions.
- Rate
or review Rational software. It’s quick and easy.
Really.
- Share your knowledge and help
others who use Rational software by writing a developerWorks article. Find out what makes a good developerWorks article and how to
proceed.
- Follow Rational software on Facebook, Twitter
(@ibmrational), and YouTube,
and add your comments and requests.
- Ask and answer questions and
increase your expertise when you get involved in the Rational forums, cafés, and wikis.
- Get social about thought
leadership. Join the Rational community to share your Rational software
expertise and get connected with your peers.





