<

SharePoint Designer 2010 - Upload a Web Part

Published on
19,411 Points
9,511 Views
4 Endorsements
Last Modified:
Awarded
Ingeborg Hawighorst (Microsoft MVP / EE MVE)
Love data viz with Excel and Power BI. Power Query rocks. Have you tried it?
SharePoint Designer 2010 has tools and commands to do everything that can be done with web parts in the browser, and then some – except uploading a web part straight into a page that is edited in SPD. So, can it be done?

Scenario

For a recent project I created a Content Query Web Part (CQWP). To fine-tune the data presentation I used customised XSL templates. Good practice suggests that these templates should not be stored in the default ItemStyle.xsl and ContentQueryMain.xsl, but in separate files. I did that and then exported the web part and edited the .webpart file to link to the custom XSL files. Then I uploaded the .webpart file to a web part page via the browser interface for testing. All good so far.

As a next step I wanted to integrate this customised web part into a Page Layout, so that new pages created with that layout would always include the customised CQWP. I did not want to use a web part page, for the following reasons:

Users with edit rights would be able to accidentally delete the web part from the page
I wanted the ability to quickly create new pages with the same layout and CQWP, without having to manually upload it into every new page
I did not want to store custom scripts calling CSS and jQuery functions in a Content Editor Web Part, but integrate them in the page header instead.

So, off we go: fire up SharePoint Designer 2010. Create a new Page Layout, create a structure with editable web part zones where users can add their own content. Finally, import the customised CQWP into the page.

But where is the command for that? The Insert ribbon has a command for web parts, but it only lists the standard out of the box web parts. A command to import or upload a web part is nowhere to be found. Searching the web yields no results, either.

I can add a new CQWP, but in order to customise its underlying XML I have to edit the .webpart file. Which can only be done if the webpart is exported. And then I can’t import the customised .webpart file into SPD. Catch 22?

Here is how it can be done:

In the SharePoint browser interface, create a web part page and import the customised CQWP. Save the web part page.
In SharePoint Designer 2010 create a new CQWP in the new Page Layout. Don’t bother with configuring the query or the presentation. Switch to code view.
In another SharePoint Designer 2010 tab, open the web part page and locate the customised CQWP. Switch to code view.

Let’s have a look at the code for the two web parts. The brand new CQWP starts with this ASP control all in one looooong line (I've added line breaks to the code so you can view it without scrolling the code snippet):

<PublishingWebControls:ContentByQueryWebPart runat="server" DataSourceID="" HelpMode="Modeless" ChromeType="TitleOnly" SuppressWebPartChrome="False" 
ListUrl="" MissingAssembly="Cannot import this Web Part." PageSize="-1" IsIncludedFilter="" ViewContentTypeId="" IsVisible="True" Title="Content Query" 
ZoneID="" AutoRefresh="False" […] ><DataFields> […]

Open in new window


The customised CQWP on the web part page starts with a different control:
<WpNs0:ContentByQueryWebPart runat="server" DataSourceID="" HelpMode="Modeless" FilterDisplayValue1="1" ChromeType="None" FilterField1="Featured" 
SuppressWebPartChrome="False" MissingAssembly="Cannot import this Web Part." PageSize="-1" IsIncludedFilter="" ViewContentTypeId="" IsVisible="True" 
Title="My Custom CQWP" ZoneID="wpz" AutoRefresh="False" FrameType="None" […] ><DataFields> […]

Open in new window


Apart from the opening (and closing) control tags, the two web parts look identical.

Comparing the two opening control tags, it soon becomes obvious that only a few modifications are required to make the new CQWP act like the customised one. The new CQWP needs to keep a few crucial settings in the opening control tag; all other parameters in the control tag can be copied from the imported CQWP. The customised CQWP has a few more parameters than the new one, but that’s understandable, since it has been customised and contains data for query and presentation.

Keep these settings from the control tag of the new CQWP:
The opening tag, i.e. <PublishingWebControls:ContentByQueryWebPart
ZoneID=""
__WebPartId="{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}"

The __WebPartId is found near the end of the opening control tag. It is a unique ID and should not be messed with.

All other parameters of the opening control tag from the customised CQWP can be copied and pasted into the new CQWP.

I found it helpful to copy the two opening tags underneath each other so I could paste the parameters from the customised CQWP to the new web part one by one.

If the web part requires script to load custom CSS or call jQuery or other JavaScript, this can be embedded in the page header. Add this control at the top of the page, above the control for the page title:

<asp:Content ContentPlaceholderID="PlaceHolderAdditionalPageHead" runat="server">
// your script goes here
</asp:Content>

Open in new window


Save the Page Layout and publish it. If this is a new Page Layout, make sure that you publish a major version, otherwise you won’t be able to use it.

In the browser interface, create a page based on the new page layout and see the Content Query Web Part in all its glory. The user cannot edit, delete, or otherwise manipulate the CQWP and all scripts required by the web part are safely tucked away in the page header.

Enjoy.

PS: The technique described in this article has been tested with SharePoint Designer 2010. The same principles should apply to SharePoint Designer 2007, though.


=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
If you liked this article and want to see more from this author,  please click here.

If you found this article helpful, please click the Yes button at the top of the page
near the label:

      Was this article helpful?

 Thanks!
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
4
Comment
3 Comments
LVL 38

Expert Comment

by:younghv
Well written and easily understood.
I am sure this will help out our SharePoint users.

"Yes" vote above.
0
LVL 50

Author Comment

by:Ingeborg Hawighorst (Microsoft MVP / EE MVE)
Thanks, younghv.
0

Expert Comment

by:Aymanit
good , and Thanks
0

Featured Post

Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

Join & Write a Comment

Key to your CPU's ability to stay cool is to use the right amount of thermal paste and apply it correctly. In other words you want as much thermal conductivity between CPU and the cooling block. Use a quality thermal paste and apply it in a manner…
Learn the basics of Skype For Business in office 365

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month