SharePoint Designer 2010 - Upload a Web Part

AID: 8490
  • Status: Published

2850 points

  • By
  • TypeTutorial
  • Posted on2011-11-03 at 18:06:41
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> […]
                                    
1:
2:
3:

Select allOpen 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> […]
                                    
1:
2:
3:

Select allOpen 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>
                                    
1:
2:
3:

Select allOpen 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!
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Asked On
2011-11-03 at 18:06:41ID8490
Tags

Sharepoint 2010

,

SharePoint Designer 2010

,

CQWP

,

import a web part

Topic

MS SharePoint

Views
2058

Comments

Expert Comment

by: younghv on 2011-11-04 at 09:01:11ID: 33037

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

"Yes" vote above.

Author Comment

by: teylyn on 2011-11-04 at 21:54:13ID: 33055

Thanks, younghv.

Expert Comment

by: Aymanit on 2012-01-31 at 21:13:26ID: 41317

good , and Thanks

Add your Comment

Please Sign up or Log in to comment on this article.

Join Experts Exchange Today

Gain Access to all our Tech Resources

Get personalized answers

Ask unlimited questions

Access Proven Solutions

Search 3.2 million solutions

Read In-Depth How-To Guides

1000+ articles, demos, & tips

Watch Step by Step Tutorials

Learn direct from top tech pros

And Much More!

Your complete tech resource

See Plans and Pricing

30-day free trial. Register in 60 seconds.

Loading Advertisement...

Top MS SharePoint Experts

  1. ACH1LLES

    358,298

    Wizard

    0 points yesterday

    Profile
    Rank: Genius
  2. JamieMcAllister

    189,161

    Guru

    0 points yesterday

    Profile
    Rank: Sage
  3. teylyn

    170,328

    Guru

    900 points yesterday

    Profile
    Rank: Genius
  4. ivan_vagunin

    156,400

    Guru

    0 points yesterday

    Profile
    Rank: Sage
  5. QPR

    123,685

    Master

    2,000 points yesterday

    Profile
    Rank: Genius
  6. Tehzar

    100,639

    Master

    0 points yesterday

    Profile
    Rank: Guru
  7. svetaye

    75,681

    Master

    0 points yesterday

    Profile
    Rank: Guru
  8. tedbilly

    71,150

    Master

    0 points yesterday

    Profile
    Rank: Genius
  9. quihong

    52,832

    Master

    0 points yesterday

    Profile
    Rank: Sage
  10. dp_expert

    49,160

    0 points yesterday

    Profile
    Rank: Wizard
  11. colly92002

    41,560

    0 points yesterday

    Profile
    Rank: Master
  12. clayfox

    39,400

    0 points yesterday

    Profile
    Rank: Genius
  13. milindsaraswala

    36,835

    0 points yesterday

    Profile
  14. RainerJ

    31,840

    1,000 points yesterday

    Profile
    Rank: Master
  15. abhitrig

    31,218

    0 points yesterday

    Profile
    Rank: Wizard
  16. dhawalseth

    30,268

    0 points yesterday

    Profile
  17. ImaCircularSaw

    29,438

    0 points yesterday

    Profile
    Rank: Guru
  18. martusha

    25,478

    0 points yesterday

    Profile
  19. zephyr_hex

    21,624

    0 points yesterday

    Profile
    Rank: Genius
  20. FastFngrz

    21,414

    0 points yesterday

    Profile
    Rank: Guru
  21. livanescu

    20,100

    0 points yesterday

    Profile
  22. danshady

    18,870

    0 points yesterday

    Profile
  23. CloudedTurtle

    18,050

    0 points yesterday

    Profile
    Rank: Master
  24. jessc7

    17,102

    0 points yesterday

    Profile
    Rank: Sage
  25. JoeKlimis

    16,318

    0 points yesterday

    Profile
    Rank: Master

Hall Of Fame