Question

I wanna do this programmatically base on the ListId and ListItemId(for Editing) so, if i wanna use DataFormWebPart i have to set the XSL Property in design mode and i don't know how to reach the XSL of Target List in Runtime and object model. i can change the target list in query string but the interface not gonna change because the XSL property is not defined:

    <WebPartPages:DataFormWebPart id="dataForm" runat="server" IsIncluded="True" NoDefaultStyle="TRUE" 
    ViewFlag="8" Title="TestParnian" PageType="PAGE_EDITFORM" ListName="{8965AE3A-CCA0-4502-B7CE-056CD670C016}" Default="FALSE" 
    DisplayName="TestParnian" __markuptype="vsattributemarkup" __WebPartId="{2CDB3F60-D76F-4410-AEDA-28D9D5815854}">
    <DataSources>
        <SharePoint:SPDataSource id="TestParnian2" runat="server" DataSourceMode="ListItem" UseInternalName="true" 
            UseServerDataFormat="True" 
            selectcommand="&lt;View&gt;&lt;Query&gt;&lt;Where&gt;&lt;Eq&gt;&lt;FieldRef Name=&quot;ContentType&quot;/&gt;&lt;Value Type=&quot;Text&quot;&gt;مورد&lt;/Value&gt;&lt;/Eq&gt;&lt;/Where&gt;&lt;/Query&gt;&lt;/View&gt;">
        <SelectParameters>
            <WebPartPages:DataFormParameter Name="ListItemId" ParameterKey="ListItemId" 
                PropertyName="ParameterValues" DefaultValue="0"/>
            <WebPartPages:DataFormParameter Name="ListID" 
                ParameterKey="ListID" PropertyName="ParameterValues" DefaultValue="{8965AE3A-CCA0-4502-B7CE-056CD670C016}"/>
            <WebPartPages:DataFormParameter Name="MaximumRows" ParameterKey="MaximumRows" PropertyName="ParameterValues" 
                DefaultValue="10"/>
        </SelectParameters>
        <InsertParameters>
            <WebPartPages:DataFormParameter Name="ListItemId" ParameterKey="ListItemId" 
                PropertyName="ParameterValues" DefaultValue="0"/>
            <WebPartPages:DataFormParameter Name="ListID" 
                ParameterKey="ListID" PropertyName="ParameterValues" DefaultValue="{8965AE3A-CCA0-4502-B7CE-056CD670C016}"/>
        </InsertParameters>
        <UpdateParameters>
            <WebPartPages:DataFormParameter Name="ListItemId" ParameterKey="ListItemId" 
                PropertyName="ParameterValues" DefaultValue="0"/>
            <WebPartPages:DataFormParameter Name="ListID" 
                ParameterKey="ListID" PropertyName="ParameterValues" DefaultValue="{8965AE3A-CCA0-4502-B7CE-056CD670C016}"/>
        </UpdateParameters>
        </SharePoint:SPDataSource>
    </DataSources>

    <ParameterBindings>
            <ParameterBinding Name="ListItemId" Location="QueryString(ID)" DefaultValue="0"/>
            <ParameterBinding Name="ListID" Location="QueryString(ListID)" DefaultValue="{8965AE3A-CCA0-4502-B7CE-056CD670C016}"/>
            <ParameterBinding Name="MaximumRows" Location="None" DefaultValue="10"/>
            <ParameterBinding Name="dvt_apos" Location="Postback;Connection"/>
            <ParameterBinding Name="ManualRefresh" Location="WPProperty[ManualRefresh]"/>
            <ParameterBinding Name="UserID" Location="CAMLVariable" DefaultValue="CurrentUserName"/>
            <ParameterBinding Name="Today" Location="CAMLVariable" DefaultValue="CurrentDate"/>
    </ParameterBindings>
        <XSL> The XSL Property that needs to be define. </XSL>
</WebPartPages:DataFormWebPart>

So DataFormWebPart not gonna help me.the next option is ListFormWebPart that doesn't need the XSL property and can be create in runtime with the object model like this:

  int itemId = listItem != null ? int.Parse(listItem.ID.ToString()) : -1;
  ListFormWebPart lstFormList = new ListFormWebPart();

  lstFormList.ListName = lst.ID.ToString("B").ToUpper();
  if (itemId != -1)
  {
     lstFormList.ListItemId = itemId;
     lstFormList.PageType = PAGETYPE.PAGE_EDITFORM;
     lstFormList.ControlMode = SPControlMode.Edit;
     lstFormList.TemplateName = "ListForm";
     lstFormList.FormType = 6;
     lstFormList.Title = "Edit " + lst.Title;
  }
  else
  {
     lstFormList.ControlMode = SPControlMode.New;
     lstFormList.PageType = PAGETYPE.PAGE_NEWFORM;
     lstFormList.TemplateName = "ListForm";
     lstFormList.FormType = 8;
     lstFormList.Title = "Add " + lst.Title;
  }
  lstFormList.ListTitle = lst.Title;
  lstFormList.AllowClose = false;
  lstFormList.AllowConnect = false;
  lstFormList.AllowEdit = false;
  lstFormList.AllowZoneChange = true;
  lstFormList.EnableViewState = true;
  lstFormList.HideIfNoPermissions = true;

  Panel1.Controls.Add(lstFormList);

this code work nicely in WebPart but not in ApplicationPage and i must say that i added all of Namespaces and References that this webpart needs, to the page.
this give me this error:

System.NullReferenceException: Object reference not set to an instance of an object. at Microsoft.SharePoint.WebPartPages.ListFormWebPart.OnPreRender(EventArgs e) at System.Web.UI.Control.PreRenderRecursiveInternal() at System.Web.UI.Control.PreRenderRecursiveInternal() at System.Web.UI.Control.PreRenderRecursiveInternal() at System.Web.UI.Control.PreRenderRecursiveInternal() at System.Web.UI.Control.PreRenderRecursiveInternal() at System.Web.UI.Control.PreRenderRecursiveInternal() at System.Web.UI.Control.PreRenderRecursiveInternal() at System.Web.UI.Control.PreRenderRecursiveInternal() at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

i put and test this code in Page_Init, Page_Load and ListFormWebPart_Init events but not any success. I use sharepoint designer and create ListFormWebPart and then copy it and pasted to my Application Page and also add the assembly needed for it, but also not any success.
i don't know what's the problem, please give the right approach if mine isn't.

Was it helpful?

Solution

First of all, OOTB WebParts are not officially supported to work on Application Pages in SharePoint. As I was told, it could be learned from official MS course 10232.

And despite of that webparts in Application Pages actually will work in some cases, there are many potential problems with them. For instance, if you provide XslLink to a OOTB XSLT-webpart, like DFWP, DVWP or XLV, it will try to cache it, and will eventually fail with exception 'Attempted to use an object that has ceased to exist'. Also, people often have issues with standard webparts Ribbon functionality on application pages, when some buttons are grayed out.

So, even if you resolve your problem today, you will probably get a couple tomorrow.

So, basically, I'd recommend you to create a site page and place your desired webparts functionality there. And if you need some additional programmatic actions, you can place your custom webpart on the same page and perform anything you need.

Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top