In the last few years i have been meeting with a lot of developers who had just been introduced to sharepoint web part development, and immediately switched to developing using SmartPart. I think it is time for me to write something about it here in the blog to help people decide when and how they should be using that solution.
I want to make one thing clear. I know that Jan Tielens put a huge effort into the smart part solution, and I think the results is fantastic. It is a great example of how one can embed an ascx control in a web part and display it in sharepoint. The addition of ajax connections between two smart parts is awesome, and I applaud it.
But while I appreciate it, and will recommend it as an example of how such things can be done, I don't think starting developers should forgo learning how web parts work and settle down to use smart part out of the box as their development platform.
I will explain.
Another small note - I gave Jan the option to comment on this article, and I am including his remarks, in green.
What is "SmartPart"?
To quote from the official (and quite out of date - Jan: I forgot about the smartpart.info site, thanks for reminding me, I’ll update it immediately.) smart part web site:
"The SmartPart is a SharePoint Webpart that can host any ASP.NET user control. Create your webparts by using the VS.NET designer instead of coding everything by hand!"
Yes- you read it right - it allows you, the frustrated sharepoint developer to create web parts by just dragging and dropping controls on a canvas, just like ASP.NET developers do it! how great is that (note - I am not being sarcastic - it is great).
Why am I saying the site is outdated? because it shows a huge link to gotdotnet, which doesnt exist anymore. The real link should be to the smartpart codeplex project, which does exist and works.
The codeplex project's home page explains that smartpart is "The SharePoint web part which can host any ASP.NET web user control. Create your web parts without writing code!" (Well, one would argue that if you want web parts without code, use SharePoint Designer - but that is opening a completely different discussion)
Smartpart is basically a simple but genius idea - it is a web part, that can host ascx controls inside it. That way, all you have to do as a sharepoint developer is to write the ascx control, and you can do it with the Visual Studio canvas - just drag and drop, and then when you want the web part on a sharepoint page, you load the generic smartpart, and tell it to load the ascx that you want. it supports ajax, and ajax web part connections - which is cool. overall - a cool solution. I totaly respect Jan Tielens, I love his blog, and his U2U CAML Query Builder is the first tool I install on every sharepoint development server I work on (or is it Patrick's?).Jan: U2U CAML Query Builder is created by Karine
The Pro is obvious -
- you gain so much in development time - you just drag and drop on a canvas
- you can easily use all the complex web controls and not worry about web part life cycle and binding times and events.
- Fantastic if you are a asp.net developer who needs to learn sharepoint development but doesnt really want to.
- The biggest con I can see is that people who use smartpart from the start do not learn about web parts and how web parts work.The solutions they create will be run in a wrapper solution and not natively on sharepoint itself. This leads to people developing user controls that require full trust from the web.config and they totaly disregard sharepoint security, resulting in solutions that require you to change the security settings in sharepoint to full trust. (Jan: If people have questions about CAS I usually point them to an old blog post (http://weblogs.asp.net/jan/archive/2005/06/23/414699.aspx), which still works for SharePoint 2007. Bottom line, deploying the SmartPart doesn’t require Full Trust on the server. WSS_Minimal should be fine! Of course it is possible that the user controls you are building will require a higher trust level.)
Another issue is with the fact that at the end of the day, the smartpart is a generic wrapper around a ascx that lies somewhere on the server. Since it is generic, and since deployment of ascx controls do not require administrative priviliges on the server, it can be used to override sharepoint security. In other words - sharepoint trusts smartpart (because it was installed by an administrator) and smartpart gives that trust to any ascx that is on the server. Combine this with the fact that learning developers do not hesitate to give full trust on the web.config without realizing what it does, and you end up with a potential security risk. Not smartpart fault - it is the fault of the developer, but my fear is that the ease of use of smart part will lead to a lot of unsecure solution like this.
A friend of mine has shown how a person with permissions to create\edit files on the sharepoint machine can use smart part to run unsecure sharepoint code. This can be corrected in the smartpart, if administrators will be able to specify in a configuration file what locations should be considered trusted by smartpart - but that would basically be repeating what .NET framework already does out of the box.
SmartPart is a wrapper. It loads another control - meaning it is another layer of code between the user and the end result. Additionaly, To expose the properties of the control it is hosting to the user it is using reflection - performance hit.
- Deployment - hard to deploy, hard to debug. I can't tell you how many people who tried to use the smartpart asked me for help on deployment and debugging. Remember, the debugging of problems in your web part now needs to also debug the smartpart code, because the problem may be in the smartpart and not your code.
Jan: I have to agree with you: debugging a user control (ASCX) with a code behind file (CS) is not as easy as debugging web parts. What I recommend people is to make use of the Web Application project template (available in VS2005 SP1 and VS2008). This will give you a DLL containing the code behind instead of a CS. You can debug that DLL just like you would debug a web part DLL. If you really want to do this in VS2005 (without SP1 or without a Web Application project) you can use precompilation.
Does not support cross page connections, and marginally supports other forms of webpart connections. If you want to support all kind of webpart connections you still need to write your own web part.
Jan: I’m not sure about the cross page connections (never tried it), but cross page connections won’t work with ASP.NET 2.0 web parts either I think (please correct me if I’m wrong). Of course flexibility comes with a price; there is only one interface implemented which will allow you to send objects between web parts. The result is that the SharePoint UI won’t prevent you from making connections between web parts that can’t work. It will be the responsibility of your code to check if the data that is received is of the correct type etc.
So what instead
Ok, so Ishai is a "bloody annoying purist who wants everyone to develop bloody hard web controls and do it all by code". That wont win me any popularity contests!
Well, my first to the question what is my genius recommendation is, "yes! develop a web control like I do". Who needs canvas? by writing the code behind we have total control of what is happening in the webpart, and we dont rely on the canvas to do what we want.
But I understand that this approach doesnt appeal to a lot of developers - and to them I suggest, just develop your own smartpart (you can start from the smartpart code! that is why its there for on codeplex, or read Eli's blog post about the Eli Robillard's blog about LoadControl), but dont make it generic - write it so that it is strongly bound to the ASCX control, and users cant use it to load other controls, or make it so that only a specific folder on the sharepoint machine is trusted by the generic webpart, and make sure that folder gives "write" permissions only for the right people. Performance will still take a hit and deployment and debugging will still be hard but at least you will be (a very small bit) more secure, and in control of what is happening.
PS: I have a friend who claims that Microsoft has a tool that generates web controls out of ASCX controls - he claims that MS used it to create most of the webcontrols that come builtin in sharepoint. I don't know if it's true or not, but if it is I hope that MS will consider publishing that tool - that would be a big step forward towards the developer experience that so many developers want.
Before I end this article, I want to add that I may update this article with more info, since I am sure I will be getting a lot of comments (online or offline) on this article. I hope everyone understands that this is not a flame against Jan and his solution - quite the opposite. I think the smartpart is a magnificent piece of code, that is very useful as an example, but I don't think it should be used without learning how it works and what it means when you use it.
What do you think? comment on this article!
If you want me to respond to you only, you will have to leave contact details, so you'd better do it through the "Email Ishai" link. I hate it when people comment on my article and say "I had a problem doing it - what am I doing wrong" and don't leave personal details how to help them.