SharePoint WebPart zoneId - sharepoint

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

Related

Need some guidance around creating a custom page to display different views for a SharePoint List.

I'm familiar with SharePoint but would like some guidance on developing a custom page.
I have 1 list and have created 3 different views.
On my Page, I have added a webpart to show the list.
I'd like to be able to add some form items - possibly radio buttons/ a picker/ or just text links at the top that would allow the user to switch views on the list below.
I'd like to add some other information and make the experience more intuitive for the user.
In short, is there a way to add a list web part and some options to choose the view of that list in another section of the page?
Sure, just do something like this
var dview = MyList.Views["YourViewTitle"];
dview.DefaultView = true;
dview.Update();
MyList.Update();
Each view has its own URL so you just need to use some HTML and JavaScript. When a box is checked or a button is clicked then you can load the URL of the view.

How to add content query webpart to publishing page layout?

How do you add a content query webpart to a apublishing page layout and configure it to aggregate content from the pages in the page library??Can some one give me some insight on this?? Do I drop a content query webpart in SP designer?? If I do that, how do I configure the query??
Or do I add a content query webpart using ribbon and then export it and make customizations and import and drop that one on the page layout??? Please someone???
SCENARIO: I have to insert a content query webpart to a publishing page layout for Physicians profile site. Based on the page layout, there will be profile pages for each physicians. Now, each physician's' profile page will have his/her primary practice location name, address, phone etc as well as other multiple practice locations. Say Mr Tom's primary location is listed as hospital 'x' on his profile page, now I want to be able to also aggregate the names of all the other lawyers that also work in hospital 'X' as Tom's office members on his bio page using content query webpart on the page layout. How do I add and configure the webpart? I mean it's easy to add a content query webpart in just one page and configure it but I want to add it on a page layout so all the content pages will have its implementation as the pages are created by the authors.
say there are 10 profile pages in the pages library and 5 of them have at least one of their pracice locations as hopital X(they may be working at hospital Y, Z..etc at the same time too), now I want all 5 doctors name aggregated on the Tom's profile pages as his office members!?
Lots of question marks... yes you can add the content query webpart to a layout by using SP designer. This will COPY that webpart to every page that is created using that layout (or has its layout switched to that one). That makes the query to set up tricky because it needs to get the input on WHICH physician from somewhere - check out this article here. Using the PageFieldValue option should do the trick.
To change a CQWP, don't modify the default version. Instead, navigate to your XSL file in the Style Library in SPD and copy the ItemStyle.XSL and rename the copy to something else (such as CUSTOMItemStyle.xsl). Then go to a test page, place a blank CQWP, and click "Export". Open the .webpart file that you exported and modify the properties to link to your CUSTOMItemStyle.xsl.
Save the .webpart file and import it back to the gallery through the browser. Rename the new CQWP (to something like CUSTOM_CQWP.webpart or whatever).
Now you can modify the CUSTOMItemStyle.xsl to use custom XSL. Shape the XSL file how your requirements dictate, then in your test page, change the Web Part properties to use your new custom ItemSTyle. Once your web part look like you want it to, you can export it again and then reimport it back into the gallery and all your settings will be saved.
Now you can go to Designer and add this web part to your page layouts. When the user uses that layout, the web part is already placed on the page and wired up accordingly. Additionally, by usign this method, they can change teh web part properties of the web part or even delete the web part altogether if they don't want it, without affecting your page layout.
http://msdn.microsoft.com/en-us/library/bb447557(v=office.12).aspx

Programmatically enumerating Web Part Zones of a Publishing Page Layout

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.

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;

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