0

I've got a list which has got two views (a list and a calendar view), which are being created when activating a feature containing following view definition for the calendar in the Schema.xml of the ListDefinition.

  <View BaseViewID="1" Type="CALENDAR" DisplayName="All appointments" WebPartZoneID="Main" MobileView="TRUE" Scope="Recursive" SetupPath="pages\viewpage.aspx" Url="AllAppointmentsCalendar.aspx" ImageUrl="/_layouts/images/generic.png">
    <ViewFields>
      <FieldRef Name="StartDate"/>
      <FieldRef Name="EndDate"/>
      <FieldRef Name="Title"/>
    </ViewFields>
    <ViewData>
      <FieldRef Name="DisplayText" Type="CalendarMonthTitle"/>
      <FieldRef Name="DisplayText" Type="CalendarWeekTitle"/>
      <FieldRef Name="" Type="CalendarWeekLocation"/>
      <FieldRef Name="DisplayText" Type="CalendarDayTitle"/>
      <FieldRef Name="" Type="CalendarDayLocation"/>
    </ViewData>
    <Query>
      <Where>
        <DateRangesOverlap>
          <FieldRef Name="StartDate"/>
          <FieldRef Name="EndDate"/>
          <Value Type="DateTime">
            <Month/>
          </Value>
        </DateRangesOverlap>
      </Where>
    </Query>
    <RowLimit>500</RowLimit>
    <Toolbar Type="Standard"/>
    <XslLink>main.xsl</XslLink>
  </View>

Creating this view will create a page containing the calendar at the URL /Lists/Appointments/AllAppointmentsCalendar.aspx

Requirement: Add an additional WebPart to the page created by the view.

To add this additional WebPart I've attached an SPFeatureReceiver and wanted to add it with the WebPartManager in the FeatureActivated method.

Question: How can I get the System.Web.UI.Page instance of the page created while creating the view in order to be able to use it to get the WebPartManager with WebPartManager.GetCurrentWebPartManager(page)?

Or is there another, better approach?

1 Answer 1

0

Going to answer my own question. Knowing the page URL, I've been able to retrieve the (limited) web part manager through the SPWeb:

    public override void FeatureActivated(SPFeatureReceiverProperties properties)
    {
        try
        {
            string relativePageUrl = string.Format("Lists/{0}/AllAppointmentsCalendar.aspx",
                                                   ElBusConstants.LIST_NAME_APPOINTMENTS);
            SPLimitedWebPartManager limitedWebPartManager =
                ((SPWeb) properties.Feature.Parent).GetLimitedWebPartManager(relativePageUrl,
                                                                             PersonalizationScope.Shared);
            if (limitedWebPartManager != null)
            {
                limitedWebPartManager.AddWebPart(new ApplyFontTagsWebPart(), "Top", 0);
            }
        }
        catch (Exception ex)
        {
            string message = "Failed to add web part.";
            SPDiagnosticsService.Local.WriteTrace(0,
                                                  new SPDiagnosticsCategory(typeof (ElBusHelper).FullName,
                                                                            TraceSeverity.Unexpected,
                                                                            EventSeverity.Warning),
                                                  TraceSeverity.Unexpected, message, ex);
        }
    }

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.