Login

How do I create a new page in EPiServer with webservices?

Versions: n/a, FAQ number: 38, Old FAQ number: 957

Q: How do I create a new page in EPiServer with webservices?

A: Here is a code example of a windows client application that uses webservices to create a sample page.

// include the web references namespace
// (we have named it EPiServer4 here)
using TestApplication.EPiServer4;


private void CreateSamplePage()
{
 // Page is added under the default start page (ID #30)
 // and the page type used is 10.
 int startPageID = 30;
 int pageType = 10;
 
 // Initialize web service communication

 EPiServer4.DataFactoryService wsFactory = new EPiServer4.DataFactoryService();

 // Set the Url property to the
 // WebServices/DataFactoryService.asmx file in EPiServer.
 wsFactory.Url = "...url here..."

 wsFactory.Credentials =
    new NetworkCredential("WebServiceAccount",
                          "password");

 // Make sure our authentication is kept alive
 // between calls.
 wsFactory.PreAuthenticate = true;


 // Create a new page under the start page.

 EPiServer4.PageReference PageRef = new EPiServer4.PageReference();

 PageRef.ID = startPageID;

 EPiServer4.RawPage rawPage =
    wsFactory.GetDefaultPageData(PageRef, pageType);

 // Set page property values...
 System.DateTime.Now currentTime = System.DateTime.Now.ToLongTimeString();

 SetPropertyValue(rawPage, "MainBody", "This is a sample page created by web services.");

 SetPropertyValue(rawPage, "MainIntro", "Sample page, created " + currentTime);

 SetPropertyValue(rawPage, "PageName", String.Format("Sample page [{0}]", currentTime));

 // Save and publish page...
 wsFactory.Save(rawPage, EPiServer4.SaveAction.Publish);
}

// Helper function for setting a property value in a raw page.
private void SetPropertyValue(RawPage rawPage,
                              string propertyName,
                              string propertyValue)
{
   RawProperty rawProperty = 
       rawPage.Property[GetPropertyIndex(rawPage, propertyName)];

   rawProperty.Value = propertyValue;
   rawProperty.IsModified = true;
   rawProperty.IsNull = false;
}

// Helper function to find out a property's index.
private int GetPropertyIndex(RawPage rawPage, string propertyName)
{         
   for(int index = 0; index  < rawPage.Property.Length; index++)
      if (rawPage.Property[index].Name == propertyName)
         return index;

   return -1;        
}

Note: remember to configure your IIS security settings, otherwise you will get access denied errors when you try to access the web services.

EPiTrace logger