Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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?

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

3 Answers 3

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>

share|improve this answer
    
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 Mar 27 '12 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 –  Stefano Paterno Mar 27 '12 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 Mar 27 '12 at 9:00

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);
            }
        }
    }
share|improve this answer
up vote 0 down vote accepted

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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