Join Robert Bogue for an in-depth discussion in this video Building Visual Web Parts, part of Developing SharePoint Full Trust Solutions for SharePoint 2013.
- Let's take a look at what it is to create a visual web part and how it differs from a traditional web part. Here we've opened up from the exercise files in chapter one, WebParts. And in this, we have the same WebParts that we've built before including our HelloWorld and a HelloYou. So, HelloWord and HelloYou are both regular web parts that have a .cs file to create the class and then a .webpart file that gets deployed. If we look at HelloVisual, this is a visual web part and it operates differently.
We still have a Hello cs, we also have our web part file and then we also have another file which is our ASCX file. This is our actual user control that is the heart of the web part. You'll see here a bunch of declaration and then simply Hello Visual World. So how does the ASCX control get deployed and then loaded into the web part so that I can build here in a visual design surface rather than having to look at code all the time.
So there's our visual design surface. If we go back to source, the way that the cs cx control gets loaded is through the HelloVisual web part. Your HelloVisual web part is a standard web part and then if you look in CreateChildControls, you'll see that it does a Page.LoadControl and ascxPath which is set up above. And you'll see that it's in CONTROLTEMPLATES/15/WebParts/HelloVisual and so on. We'll take a look at how the file gets deployed there in just a moment. But that loads the control and then it adds it to the inside of this wrapper that is the visual web part.
So if these all get deployed there, well how does that work? What happens is in our Feature, you'll see that hello visual has set a files and those files include its Element manifest for the files necessary for it. It's .webpart file that gets deployed and it's UserControl.ascx. If we look at the elements file, we'll see that it's being deployed just in the web part catalog.
And there's no reference here for the ASCX control. That's because the ASCX control's getting deployed in _layouts and so it doesn't get packaged into the Feature definition. It gets packaged into the package itself. So once we have the package open, we can look at the Manifest and we'll see that there is a TemplateFile that is being put into CONTROLTEMPLATES\WebParts\HelloVisual\HelloVisual.ascx So, the actual package contains the instructions on where the ASCX should go and then it's hard coded into the visual, HelloVisual.cs file exactly where that maps to.
So, we've got the ASCX control being deployed through the package into a location that's available everywhere and it's being loaded from the visual web part. If we hit a five to package or compile and package and deploy, we can throw our page into Edit mode, hit Insert, add our web part. And see our Hello Visual World come up. So, visual web parts are really web parts that contain the user control that you can do in a design surface.
Let's quickly review what a visual web part is. It's the WebPart itself that does a LoadControl to the ASCX which is deployed on everyone one of the SharePoint Servers. It allows us to do user interface design for web parts rather than having to manually add controls to the control tree. One thing that's important as we're working with visual web parts is there are no properties on the actual .webpart itself so we'll have to plumb those down into our ASCX control if that's something that we need.
If you've wanted to learn how to develop custom solutions on SharePoint—or you've been told that you need to develop a solution that integrates with SharePoint—this is the training you need. It's quick, to the point, and effective at helping new developers work with SharePoint code.
Robert Bogue shows how to create new web parts, access and update data in SharePoint, work with files, and create SharePoint artifacts: lists and libraries to store data, site-level features, files, etc. He also shows how to create sandbox solutions that do not affect the rest of the server environment, and discusses execution options for solutions, including in-page, quasi-page, and out-of-page execution.
- Developing a web part
- Connecting and configuring web parts
- Building sandboxed code
- Choosing an access method for SharePoint data
- Querying with CAML
- Fetching files
- Updating SharePoint data
- Creating and provisioning artifacts
- Executing long-running operations
- Tinkering with timer jobs
- Understanding SharePoint execution workflow