Programmatically enumerating Web Part Zones of a Publishing Page Layout - sharepoint

I have a situation where a class I have is passed a PublishingPage instance and I want to enumerate any and all web part zones that are used in the Layout Page for this page.
This is proving tough.
PublishingPage has a property called Layout, which is of type PageLayout. This is different from PublishingLayoutPage which inherits (eventually) to Page which has the Zones property. This PageLayout type does not have any reference to the normal ASP.NET Page type which has the Zones property I need.
I can get an SPListItem and a SPFile from the PublishingPage.Layout property, and get the contents of the page layout in raw text form, but this can't be parsed with XmlReader as it's not valid XML (has <% tags which are invalid).
I can get an SPWeb from PublishingPage.ListItem.Web, and this can get me an SPLimitedWebPartManager, where I can get a collection of LimitedWebParts (which has Zone information) - but this won't help because at this stage of my code I have no web parts.
I'm pretty stuck, it looks like the Publishing Infrastructure is quite detached from both ASP.NET and SharePoint.
UPDATE:
I can use the SPLimitedWebPartManager to add a dummy web part (like a new ContentEditorWebPart) to a web part zone (I have the names of the zones that are used), but when I save this out and check the page back in (and get a new SPLWPM), I can get the WebParts back but the Zone property is null (the ZoneID property is populated and correct).

The posts from Wayne Fan on this thread seem to cover what you're trying to do. Hope this helps.

Related

SharePoint WebPart zoneId

I am using SharePoint online.
I want to use this CSOM code to add a WebPart to a page:
SP.File oFile = _web.GetFileByUrl(SiteUrl + "/SitePages/" + pageName);
oFile.CheckOut();
LimitedWebPartManager limitedWebPartManager = oFile.GetLimitedWebPartManager(PersonalizationScope.Shared);
var importedWebPart = limitedWebPartManager.ImportWebPart(webPartSchemaXml);
var webPart = limitedWebPartManager.AddWebPart(importedWebPart.WebPart, zoneid, zoneIndex);
oFile.Update();
await SiteCtx.ExecuteQueryAsync();
oFile.CheckIn(String.Empty, CheckinType.MinorCheckIn);
The problem is how to assign correct values to the zoneid string variable,
which is the name of the Web Part zone to which to add the Web Part.
When I run this code nothing happens!
(it doesn't add the WebPart to the page and I am suspecting it is related to the wrong zoneId).
I have read various post, ranging from accessing the code behind of the .aspx page trying to find the WebPartZone, accessing the WebPartManager class (which should list the ZoneId's but I don't know how to get it, since that I am using the LimitedWebPartManager class).
I have tried various values for zoneId, but at the moment none of them work:
Zone 1 (just a guess!)
Zone 2 (i see it in the right tab when manually editing the webpart through Edit page)
Body (with this the code worked some days ago! but now it doesn't anymore)
Header
Left
Bottom
What is the proper method of findind zoneId's?
EDIT
The page is the homepage, I have read somewhere that it is a wiki page so maybe it has different ZoneId's.
ZoneIDs might be different depending on the page layout, but usually out-of-the-box SharePoint layouts use the following ID's for webpart zones:
TitleBar
Header
LeftColumn
MiddleColumn
RightColumn
Footer
Make sure that you invoke the update() method on your file object and executeQueryAsync() method on your context after importing the webpart - the latter function especially is responsible for sending the request to server and applying your changes.
Here's a nice article about adding webparts to pages programatically: How to programmatically add a ClientSide Web Part to a SharePoint page

Accessing properties across pages in AEM HTL (Sightly)

I have a carousel component, whose slide content will be the content from another page, accessed via reference component. I want to get some properties authored in the carousel to be accessible inside the referenced page. i.e. I have Page A- with a Carousel accessing PageB through reference component. I want to get proeprties from PageA in PageB. Can this be achieved?
Sure it can, you will need to lookup/query the content and find pages where PageB is included, then get the properties of those pages (there might be more than one) and merge them. It will have to be custom code though as this is not available out-of-the-box as of AEM 6.3.
The solution could be adding a query parameter or setting a request parameter. But then again, this depends on the requirement. For me, this worked!!

Need some help with sharepoint web part development

I have a custom web part where there is a text box. My custom web part also has a custom setting where user enters some values separated by commas. So on load, the UI should display the values entered in the setting (without commas though).
Is this possible with normal ascx user control, since it cannot access the sharepoint web part property at the time of creation and hence cannot display the message on load.
Need some help to solve this.
Thanx
I assumed you're probably doing something like this.
What you'll need to do is pass your web part properties to your usercontrol. You can do this by creating public properties in your usercontrol class (demonstrated in the link I give above).
Code sample from the article:
// Loads a user control
MyUserControl myUserControl = (MyUserControl)Page.LoadControl("~/_controltemplates/MyWebPart/MyUserControl.ascx");
myUserControl.Web = SPContext.Current.Web;
myUserControl.TextColor = this.TextColor;

Difference between SPFile and PublishingPage class

What are the difference between SPFile class and PublishingPage Class? I am trying to manipulate a SharePoint Page and I see that both SPFile and PublishingPage classes can be used and the only difference I see if PublishingPage is used for aspx file and SpFile can be used for anyfiles in a SharePoint site. I also see that there are few extra properties like ContactEmail etc in PublishingPage.
Is there anything important I am missing here?
You pretty much answered your own question. the PublishingPage class is used to represent pages authored by end-users in a publishing site. It has information about the page layout used, the content type upon which the page layout was based, when the page should be published, and so on. The SPFile object is used to represent any file in a library.

Using a MulitpleLookupField in MOSS '07 Layout Page

I have a page layout for my MOSS '07 site that I want put a MultipleLookupField in. The field will point to a multiple lookup column in my custom content type that points back to the pages library so I can have a "Related Articles" field.
I've gotten the field to show up correctly--it's even editable when the page is in edit mode! But when you click on the link that shows up there, it display the page's properties view instead of the page itself.
There's lots of properties on the control, but there's little documentation at MSFT as to what they do.
Does anyone know how to change the link's URL to the actual page instead of the properties view?
Here's the tag I'm using:
<SharePointWebControls:MultipleLookupField
ID="MultipleLookupField1"
FieldName="RelatedIssues"
runat="server"></SharePointWebControls:MultipleLookupField>
The link takes me to here: /Pages/Forms/DispForm.aspx?ID=6&RootFolder=*
I want it to take me here: /Pages/faq1.aspx
Unfortunately the lookup control is designed to be compatible with generic lists and does not understand that the document libraries have a file with a specific url associated.
You will probably have to find a custom lookup control on the web or roll your own.

Resources