0

I'm creating SPWebs based on WebTemplates, which each one have is own, for the welcome page, have modules for the pagelayout and default.aspx page.

At this point I'm using a PublishingLayoutPage and showing some metadata fields of the page created from this page layout as well with a webpart zone.

But now I need to embedded in the same default.aspx the docLib view web part of a specific docLib of that SPWeb (docLib will always have a diff. internal name)

What is the most easy and correct approach for initialize this custom webPart?

I've checked this link, but I think that is not the ideal approach for what I want. I think that what I need is less complex.

ideas?

2
  • So you can't add a webpart based on something like "Shared Documents"?
    – Mike
    Commented Mar 26, 2012 at 14:13
  • yes, but the docLIb with a custom internal name (url name).
    – fallout
    Commented Mar 26, 2012 at 15:03

3 Answers 3

1

If you are using onet.xml in your SiteDefinition you can use the following code:

<Module Name="DefaultBlank0" Url="$Resources:osrvcore,List_Pages_UrlName;" Path="">
  <File Path="default.aspx" Url="default.aspx" Type="GhostableInLibrary">
    <Property Name="Title" Value="Intranet" />
    <Property Name="ContentType" Value="$Resources:cmscore,contenttype_welcomepage_name;" />
    <Property Name="PublishingPageLayout" Value="~SiteCollection/_catalogs/masterpage/MySiteListingLayout.aspx, Home Page" />
    <Property Name="PublishingPageContent" Value="Hi all" />
    <AllUsersWebPart WebPartZoneID="MainTopListing" WebPartOrder="0">
      <![CDATA[
        <webParts>
          <webPart xmlns="http://schemas.microsoft.com/WebPart/v3">
            <metaData>
              <type name="YourAssembly.WebParts.common.NewsWebPart.NewsWebPart, YourAssembly, Version=1.0.0.0, Culture=neutral, PublicKeyToken=37b39a266ae2abb5" />
              <importErrorMessage>Error importing the Web Part.</importErrorMessage>
            </metaData>
            <data>
              <properties>
                <property name="Title" type="string">News Web Part</property>
              </properties>
            </data>
          </webPart>
        </webParts>
      ]]>
    </AllUsersWebPart>

3
  • but like that, I'm just creating the webpart zone, right? I need to add a docLib web part view. (sorry the basic doubt)
    – fallout
    Commented Mar 27, 2012 at 7:56
  • You prepare the pagelayout with WebPartZone and then in sitedefinition (Onet.xml) can enter the code you've written, or under provisioning through a eventreceiver you can hook the webpart in the WebPartZone objectmodel Commented Mar 27, 2012 at 8:49
  • using this example: code.msdn.microsoft.com/Adding-default-web-parts-ab7aec72 for add the WP to the default page? that's it?
    – fallout
    Commented Mar 27, 2012 at 9:00
1

For example i use this code:

PublishingPage publicProfilePage = sps.AddPublishingPage("Public.aspx", layout);

                var wpm = publicProfilePage.ListItem.File.GetLimitedWebPartManager(PersonalizationScope.Shared);

WebPartsHelper.AddWebPart(wpm, "ProfileWebPart", "MainLeftColumn", 0);

and the static method AddWebPart:

public static void AddWebPart(SPLimitedWebPartManager wpManager, WebPartParam webPart, string zoneId, int zoneIndex) { XDocument webPartXmlSettings = GetWebPartFile(wpManager, webPart.WebPartName); if (webPartXmlSettings != null) { try { XmlReader xtr = webPartXmlSettings.CreateReader(); var errorMsg = string.Empty; System.Web.UI.WebControls.WebParts.WebPart newWebPart = wpManager.ImportWebPart(xtr, out errorMsg);

                // Web part values initialization.
                object values = webPart.WebPartValues;
                if (values != null)
                {
                    Type webPartType = newWebPart.GetType();
                    foreach (PropertyInfo p in values.GetType().GetProperties())
                    {
                        PropertyInfo wpProperty = webPartType.GetProperty(p.Name);
                        if (wpProperty != null)
                        {
                            wpProperty.SetValue(newWebPart, p.GetValue(values, null), null);
                        }
                    }
                }

                newWebPart.ChromeType = System.Web.UI.WebControls.WebParts.PartChromeType.None;
                wpManager.AddWebPart(newWebPart, zoneId, zoneIndex);
            }
            catch (Exception ex)
            {
                SPDiagnosticsService.Local.WriteTrace(0,
                    new SPDiagnosticsCategory("WebPartsHelper.AddWebPart() Error", TraceSeverity.Medium, EventSeverity.Error),
                    TraceSeverity.Unexpected, ex.Message, ex.StackTrace);
            }
        }
    }
0
0

I reach this first concept solution, probably not the best one but its enough for now.

public static void AddListViewWebPart(SPWeb oWeb, SPFile oFile, SPList oSPList, string webPartTitle, string webPartZone)
{
    bool state = oWeb.AllowUnsafeUpdates;
    oWeb.AllowUnsafeUpdates = true;

    //Create the object SPLimitedWebPart Manager
    SPLimitedWebPartManager limitedWebPartManager = oFile.GetLimitedWebPartManager(PersonalizationScope.Shared);

    //Create the object of ListViewer webpart
    var oListViewWP = new ListViewWebPart
                          {
                              ChromeType = PartChromeType.TitleOnly,
                              Title = webPartTitle,
                              ListName = oSPList.ID.ToString("B").ToUpper()
                          };

    //Define the zone in which webparts need to be added
    limitedWebPartManager.AddWebPart(oListViewWP, webPartZone, 1);
    oWeb.AllowUnsafeUpdates = state;
}

Thanks for your help Stefano.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.