SharePoint Publishing WebService integration

There are many ways to customize and integrate with/to SharePoint 2007 (MOSS).

With the migration of Microsoft Content Management System (MCMS) into SharePoint, the ability to create a publishing page using a template can allow non-technical users to create some dynamic-looking & functioning collaborative-ly created web sites.

While it is super-easy for content authors to add and update pages within the website, there is also the ability to do so via the SharePoint API.

This opens up the content management & publishing functionality to integrate to back-end systems, and other solutions, you could have (for example) :

  • Microsoft CRM publishing a page detailing certain fields as a Customer showcase
  • Custom application/database maintaining a list of hotels for rent/sale, allowing publishing of details, photos & search to the web site

This means that existing solutions and application within the enterprise won’t need to move any “data” to the SharePoint environment, nor should they.

Instead, the office user would simply check a box in the WebApp (or other system UI), or click a button to have the details available online.

Of course, this could then show in whichever page layout (template) format as desired, complete with CSS and HTML layout, etc – and only the fields needing to be published (by the application/web service).

You could then configure “search” functionality within SharePoint – and you have a searchable online website, driven from a existing core business system.

< WARNING : Lots of C# code & SharePoint jiggery-pokery ahead >

The basic premise is to have a Web Service, with the functionality to create a “Publishing Page” using the SharePoint API.   So – let’s do that…

I’m working with Visual Studio 2005 (could use 2008 instead) on a VM that has SharePoint installed, and the full developer tools – it’s best way to get a working developer environment.

Create Web Service

In my example, the class/web service is called GWCS – Grumpy Wookie Car Sales – could then use something like a C# Windows App used to push listings of new & used cars, including photos (eventually).

Add References

Need to add some DLLs (references), and include as references in the C# class :

Navigate to > C:Program FilesCommon FilesMicrosoft Sharedweb server extensions12ISAPI

Add DLL’s :

  • Microsoft.SharePoint.Publishing.dll
  • Microsoft.SharePoint.dll

And – include the namespaces :

using Microsoft.SharePoint;
using Microsoft.SharePoint.Publishing;
using Microsoft.SharePoint.Publishing.Fields;

Include Method Params

As the interface to the web service, I’m using some hard-coded input variables, rather than a class library – which would be much nicer if you need more than 1/2 dozen inputs.

public string PublishCarPage( string strGWCSPath, string carRego, int intCarType, string strTitle, string strDescription, string strMake, string strModel)

Some quick variables :

string result = string.Empty;
PublishingPage pubPage = null;

Load SharePoint Publishing Site

Just need to create a reference to the “site” initially – this is the specific site collection that you would have created in Central Admin – in my case, it’s my Car Sales site on the internal “Wookie-Sell-It.com” server.

SPSite spSite = new SPSite(“http://wookie-sell-it/sites/GWCS/”);

Next up, open the “site” within the site collection.   This would be for any nested sites, or “children” sites – depending on your corporate terminology.

I’m opening the “UsedCars” site, for GWCS – there is also a “NewCars”, “Trucks”, etc. 

Can use some business logic to put in different “folders” – and thus could have different master pages, page layouts, and search function – for example.

if (intCarType == 1) SPWeb spWeb = spSite.OpenWeb(“UsedCars”);

Next, have to grab a “PublishingWeb” object type :

PublishingWeb pubWeb = PublishingWeb.GetPublishingWeb(spWeb);

Check Page Exists

The page I’m going to create will have the URL for the registration number for the car, using the input param :

string strGWCSPage = strRego + “.ASPX”;

Need to do a quick loop through the Pages collection – and see if it exists :

foreach (PublishingPage page in pubWeb.GetPublishingPages())
{
    // Check if page exists. If it exists, update it
    if (page.Name == strGWCSPage)
    {
        pubPage = page;
        break;
    }
}

Check-Out

If found, then need to check it out, prevent other authors editing :

if (pubPage != null)
{
    pubPage.CheckOut();
}

…or Create Page -> Page Layout

Otherwise, it doesn’t exist – so we need to create it. 

Have to firstly specify which “page layout” we’re creating from – this needs the “PublishingSite” reference – I thought it could work from the PublishingWeb object, but different :

else
{
    PublishingSite pubSite = new PublishingSite(spSite);

There are a bunch of Page Layout “types” from the SharePoint gallery – take your pick !    Or – can use a custom Page Layout (most likely).

I’m just being lazy, and using the “1st” one…

PageLayoutCollection pageLayoutCollection = pubSite.GetPageLayouts(true);
PageLayout pageLayout = pageLayoutCollection[1];

Here’s a list of the specific “out of box” ones :

0 – “The article page with body only contains a rich text field.”
1 – “The article page with image on left contains an image field and a rich text field.”
2 – “The article page with image on right contains an image field and a rich text field.”
3 – “The article page with links contains an image field and summary links.”
4 – “Page layout for creating web part pages”
5 – “This page layout contains a redirect control for automatically directing readers to any specified URL.”
6 – “The welcome page with summary links contains an image field on left, a rich text field, 2 summary links, and Web Part zones arranged in a header, a footer, and 2 columns.”
7 – “The welcome page with table of contents contains an image field on left, a rich text field, a table of contents Web Part, and Web Part zones arranged in a header, a footer, and 2 columns.”
8 – “The welcome with splash contains an image field on left, a rich text field and Web Part zones arranged in a header, and 2 columns.  The left navigation pane is hidden.”

If that’s successful, then can create a “page” using the “page layout” :

if (pageLayout != null)
{
    pubPage = pubWeb.GetPublishingPages().Add(strGWCSPage, pageLayout);
}

Populate Content

All that’s left – is to fill in the content !     Fields, images, whatever – as it’s a Page Layout specified “graphically” by a web designer, it’s just fill in the gaps (properties).

pubPage.Title = strTitle;
pubPage.Description = strMake + ” – ” + strModel;

And – the “Page Content” which has a bunch of HTML :

pubPage.ListItem[“Page Content”] = “<H1>” + strRego + @”</H1><BR><H2>Come and have a look at :” + strDescription + @”</H2>”;

And some custom properties – these were added as Site Columns, but a better approach would be to add to Content Type – then assign it to the Pages Library you’re using.

pubPage.ListItem[“CarMake”] = strMake;
pubPage.ListItem[“CarModel”] = strModel;

Push it up to SharePoint

Have to update the “listitem” :

pubPage.ListItem.Update();

And – set to update the “page” object, does some more stuff above & beyond the listitem update. 

And then Check-In – and it’ll appear on the website.

pubPage.Update();

pubPage.CheckIn(“Checked in by GWCS WebService”);

So – here it is in action :

(1)  The site with no cars listed (yet):

image

(2) Next – will use the Web Service to add one :

image

(3) Check back in SharePoint

image

And – within the “All Site Content” view, you can see the additional properties – these would be most likely be added to a page layout – designed via SharePoint Designer (for example) :

image

And – if you look at the “version history”, you can see the comment as included in the web service code :

image

</end>

———————————————

IMPORTANT :

The user account that the web service is “running as” must have some HIGH level privileges for this to work – basically, as much as the MOSS user account.

I created a new web app, and set the same AppPool as for SharePoint.

Lastly – this nasty error held me up for a long time (!) :

The security validation for this page is invalid. Click Back in your Web browser, refresh the page, and try your operation again.

I was finally able to get it work after turning off “security validation” for the website, as detailed by SpiderWool.

Navigate thru the following and turn the security validation Off

Central Administration

> Application Management

> Web Application General Settings

image

 

Will update this post soon, with some more “properties” – such as loading up an IMAGE – as well as source code, example code (C# only)

UPDATE : Click here to view the full code sample.

Advertisements

3 thoughts on “SharePoint Publishing WebService integration

  1. I’d like to thank you for the efforts you have put in penning this website.
    I really hope to see the same high-grade blog posts from you later
    on as well. In truth, your creative writing abilities has inspired me to
    get my own site now šŸ˜‰

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s