Friday, November 18, 2005

A Web service to create Postings

There can be situations where the only solution to access MCMS is via web services. KB 327750 (How to use an MCMS Web service to ...) can be used as a starting point on creating a web service for MCMS. This article explains how a posting can be created using a web service (only the web method is provided).

[WebMethod]
public bool CreatePosting(string userName, string password, string channelGuid, string postingName, string templateGuid, out string error)
{
    //The above parameter list can contain more information about the posting such as display name, placeholder content, etc.
    //It is always recommended to encrypt the password and decrypt it.
    //Usually error will be very much useful to get the exact problem form MCMS.
    error = "";
    bool postiongCreated = false;
    try
    {
        using(CmsApplicationContext cmsContext = new CmsApplicationContext())
        {
            //Authenticate the context in update mode
            cmsContext.AuthenticateAsUser(userName, password, PublishingMode.Update);
            //Get the channel object
            Channel parentChannel = cmsContext.Searches.GetByGuid(channelGuid) as Channel;
            //Get the template object
            Template sourceTemplate = cmsContext.Searches.GetByGuid(templateGuid) as Template;
            //To check whether they are found or not. If necessary it can be separated
            //to two conditions
            if(parentChannel!=null && sourceTemplate!=null)
            {
                //Can this user create a posting/this template can be used to create posting?
               //If necessary it can be separated to two conditions
                if(parentChannel.CanCreatePostings && sourceTemplate.CanUseForAuthoring)
                {
                    try
                    {
                        Posting newPosting = parentChannel.CreatePosting(sourceTemplate);
                        //More information can be set here if available.
                        newPosting.Name = postingName;
                        //Commit the changes
                        cmsContext.CommitAll();
                        postiongCreated = true;
                    }
                    catch(Exception creationException)
                    {
                        //Oops.. An error occurred. Rollback!
                        cmsContext.RollbackAll();
                        error = creationException.ToString();
                    }
                }
                else
                {
                        error = "User " + userName +" don't have rights to create a posting in "+ parentChannel.Name + " channel or the template " + sourceTemplate.Name + " cannot be used for authoring.";
                }
            }
            else
            {
                error = "Channel/Source Template not found";
            }
        }
    }
    catch(Exception generalException)
    {
        error = generalException.ToString();
    }
return postiongCreated;
}

2 Comments:

Anonymous Anonymous said...

hi Chester,

I´m a newbie in MCMS developement and i've used your code to create post programatically, but when i go to site manager to see the new post it appears with locking owner as "the user i create for do it", this means that only that user can edit or publish it or is posible that MCMS administrator can edit and publish it?

Thanks in advance!

10:32 PM  
Blogger Chester said...

Hi,

Not relly. But if you want it to be editted by everyone, then you should relese the lock on the posting.

Cheers,
Chester.

8:34 PM  

Post a Comment

<< Home