views:

223

answers:

2

I have a custom site definition, in an ONET.XML file. Can I get the URL and/or the Site name of the site that is being created and use it as a parameter/moniker in the onet.xml file?

Something like this:

 <Modules>
    <Module Name="Default" Url="" Path="">
      <File Url="default.aspx" NavBarHome="True">
        <AllUsersWebPart WebPartZoneID="Right" WebPartOrder="1">
          <![CDATA[
                   <WebPart xmlns="http://schemas.microsoft.com/WebPart/v2" xmlns:iwp="http://schemas.microsoft.com/WebPart/v2/Image"&gt;
                        <Assembly>Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c</Assembly>
                        <TypeName>Microsoft.SharePoint.WebPartPages.ImageWebPart</TypeName>
                        <FrameType>None</FrameType>
                        <Title>[NAME OF MY SITE] or [URL]</Title>
+1  A: 

Not to the best of my knowledge. I have used a solution where I add the webparts programmatically via the WebPartManager and then to set properties on that WebPart I use reflection.

An example to add the WP to a page:

public static void AddWebPart<T>(string webPartZone, SPFile page)
{
    Type wpType = typeof(T);
    AspNet.WebPart webPart =
        (AspNet.WebPart)wpType.GetConstructor(new Type[0]).Invoke(new object[0]);

    try
    {
      page.CheckOut();                                               
      SPLimitedWebPartManager wpManager = 
         page.GetLimitedWebPartManager(AspNet.PersonalizationScope.Shared);
      wpManager.AddWebPart(webPart, "WP Zone Name", 0);
      page.CheckIn("Added web part", SPCheckinType.MajorCheckIn);

and here is how to set proprties on webparts:

public static void SetWebPartProperties<T>(Dictionary<string, string> properties,
                                           SPFile page)
{

    Type wpType = typeof(T);

    SPLimitedWebPartManager wpManager = 
    page.GetLimitedWebPartManager(AspNet.PersonalizationScope.Shared);

    foreach (AspNet.WebPart wp in wpManager.WebParts)
    {
        if (wp.GetType() == wpType)
        {
            foreach (KeyValuePair<string, string> kvp in properties)
            {
                System.Reflection.PropertyInfo pi = wpType.GetProperty(kvp.Key);
                if (pi.PropertyType == typeof(int))
                    pi.SetValue(wp, int.Parse(kvp.Value, CultureInfo.InvariantCulture), null);
                else if (pi.PropertyType == typeof(bool))
                    pi.SetValue(wp, bool.Parse(kvp.Value), null);

                pi.SetValue(wp, kvp.Value, null);
            }
            wpManager.SaveChanges(wp);
        }
    }
    page.CheckIn("Changed properties of webpart", SPCheckinType.MajorCheckIn);

}
Johan Leino
Yes, this is what I ended up doing as well. However, since I only needed to update one property on the web part with the site name in this case, I hoped for the possibility to do it the easy way.
Magnus Johansson
A: 

I don't know of any way to do this with ONET.XML.

I'd recommend using a feature to add the web part. You can use feature stapling to associate the feature with a site definition. This way, when a site is created based on your site definition your feature is automatically activated.

In your feature you can write code to programmatically add your web part and you'll be able to access properties of the SPWeb (i.e. the site) and get its URL or site name as required.

dariom
Yes, I already have that mechanism in my solution. Since I only needed to set a property on the web part to the site title, i was hoping for an easy fix.
Magnus Johansson