Provisioning Web Part pages and WebParts

Introduction

Hello,

Just allow me to begin with few lines of introduction this time to explain why I prefer talking about provisioning web parts and not about any other specific topic, although I’m still trying to share what I’m learning and I think that there’s still too much technics to talk about.

After 2 years of developing SharePoint applications, I think that the way how development teams do things depends on their experience and certainly what they know, what they don’t, the way how they are organized sometimes…. There are certainly too many other conditions that we should not forget about ….. But to explain what I’m trying to say, it depends also on the way how they are TRYING to do things.

Talking about SharePoint Development, beginners start with few tutorials or samples and try then to practice these walkthroughs exactly as they were explained, step by step, in order to get their first demo working. Developers with much more experience, or professionals try to make things look better and to find more advanced technics to automate even the process or all the required steps to get the same result or even to make it look better, as I said.

We start all by developing web parts, once they are deployed to the site we should edit the already existing page or create a new one and insert the web part in the right web part zone, where it should be displayed. That’s perfect, it’s working! But what if we can automate things, this way you just have to deploy the solution or the web part, and that’s all, check your page and here’s your web part, already displayed. That should be awesome too. You don’t think so?

It’s just one simple example I know, we are talking about how to provision web parts and there are too many other topics to talk about too but I think that you should go this way.

Talking about deployment on the production servers now, because this final step depends also on the way how your application was designed and developed. I’m still trying to explain how to “automate deployment process”, so whatever was the method with which you used to do that, this way you don’t need to do any manual configurations or to manually define some settings or even to add some required assemblies or whatever was what you should do, you just need to launch the PowerShell script (only one .bat file) and wait until your solution(s) gets installed and your application gets provisioned, configured and “ready to use”.

With all my respect for the other deployment methods or approaches (using content database for example, this way we create a new web application to which we attach the restored database once it’s back upped from the development environment’s SQL Server …you should install the .wsp solutions too) I don’t want to talk about site definitions or web templates or any other site provisioning methods it’s not about that I’m just trying to explain how things should be done, I mean, and just for example, never edit manually the web.config file to add some configuration settings, that should be done using the “SPWebConfigModification” class so your modifications gets persisted too, never deploy assemblies manually you can do that using the package designer, don’t add manually files where ever you want to make them, use instead Modules for example or Mapped folders or the deployment type property, just keep all that in mind and try to organize your features and your solution(s)…your feature dependencies your feature event receivers if you need to execute something once the feature gets installed, activated….that’s it ! Use SharePoint development tools and features and all what SharePoint offers to you, to develop your SharePoint applications. This way you can even plan your upgrade scenario(s) or make this step easier anyway even If you don’t need to do it right now, but things become more organized and easy to maintain.

I have just talked about all that (and I’m still talking, I know I’m talking too much) because the way how you will do things tomorrow, depends on the way how you are doing them today or even how you did them yesterday, so think about your SharePoint application this way, otherwise you will finish with too much problems.

Provisioning Web Parts Declaratively:

Starting with the declarative approach.

Our solution is the following:

1

  • WebPartToProvision : a simple visual web part, just to show one message telling us that it is provisioned declaratively into the page. This web part will be deployed through the site collection scoped feature called “Web Part To Provision”
  • Module For Provisioning Web Part : a module that contains the web part page to be deployed to the site pages library with the required configuration to show the visual web part. This module is included in the web scoped feature called “Provision Web Part”

That’s all what we need in our demo, until now.

We suppose that we just need to deploy our “DemoPage.aspx” to the “Site Pages” library, and to call it “VWDemoPage.aspx”, the module content should be the following :

2

Trying now to provision our visual web part. Notice that our page contains 3 web part zones as shown below. We want the visual web part to get displayed in the third web part zone with ID = “Body”.

5

To do so and to provision the visual web part declaratively through the Module, we need to add the required configuration as shown below :

3

  • AllUsersWebPart tag : contains simply the web part to provision through the module
  • WebPartZoneID : contains the ID of the third web part zone In our demo page, in which we want the visual web part to get displayed, as you can guess J
  • WebPartOrder : to specify the vertical positioning of the web part within the zone
  • WebParts tag is the xml defined in the “WebPartToProvision.webpart” file already created within the web part

Pretty straightforward …no ? I think it is.

So deploy the solution, check your “Site Pages” library, find and open the “VWDemoPage.aspx”.

I don’t think that you will get the same message with the same applied style 🙂 since you don’t have my demo solution but you will get certainly your web part provisioned and displayed in the web part page.

That’s it ! really simple !

4

May be you are wondering, if I can guess, now what about provisioning sandboxed web parts ?

Exactly the same thing!  That’s it!

Assuming that you have created your sandboxed web part, you want to provision it as I did, in a new page and using the same module and the same DemoPage.aspx page, your module elements.xml file should be something like this:

6

Trying now to provision it in the first created page in which we displayed the visual web part, but for this sandboxed web part, we choose to display it in the “Header” web part zone. This time your module xml should be the following:

8

If you did it correctly you will get your web parts displayed in the same page, the first one (the sandboxed web part that shows the site lists in a drop down list in my sample) in the “Header” web part zone, and the visual web part will be displayed in the “Body” web part zone.

9

That’s all! Hope that was helpful!