- It seems when you deploy a feature containing web parts, you still have to add it to the web part gallery for each site.
- If you want to reference controls in the CONTROLTEMPLATES directory, you can use the virtual path ~/_controltemplates.
- If your controls have a code behind that is strong named, you must fully qualify the Inherits attribute of the @Control directive.
- To remove broken webparts from a page, add the query string ?contents=1 to the URL. (Via http://sharepointinsight.blogspot.com/2008/06/remove-bad-or-broken-web-parts-from.html)
- To disable friendly error messages (and see the yellow screen of death)
- set the CallStack attribute of the SafeMode node to true
- set CustomErrors mode attribute to Off
- set debug attribute of the Compilation node to true
Wednesday, August 19, 2009
WebPart Notes
SharePoint Workflow Notes
Here are some notes from my experience building a SharePoint state machine workflow.
- All fields delcared on the workflow object must be serializable type or marked nonSerializedAny activities before the workflow activated event will not have access to workflow properties
- Remember to remove dll's from the GAC when building, GAC'ed versions will supersede local versions
- Emailing from SharePoint requires the network service account to have local activation permission for "IIS WAMREG Admin Service" (a dcom service): http://www.cleverworkarounds.com/2007/10/25/dcom-fun-with-sharepoint/
- There is a bug in the delay activity, here are some patches: http://blogs.msdn.com/sharepoint/archive/2008/07/15/announcing-availability-of-infrastructure-updates.aspx, http://support.microsoft.com/kb/953630/
- SetSate is not like a return, the rest of the activities in that phase will still execute
- You cannot have more than one list template in a feature
- Set workflow logging level use this command:
- stsadm -o setlogginglevel -category "Workflow Infrastructure" -tracelevel verbose
- To get and set timer settings use this command:
- stsadm -o getproperty -pn job-workflow -url http://localhost/
- stsadm -o setproperty -pn job-workflow -pv "Every 1 minutes between 0 and 59" -url http://localhost/
- To allow unsigned powershell scripts to run use this command:
- Set-ExecutionPolicy Unrestricted
Workflow Versioning
- There is no concept of versioning for Visual Studio created SharePoint workflows
- Instead, include the version in the feature path, name, description and workflow name and description
- Change the assembly version, and the version in each of the other places
- Deploy as a new feature
- Make sure the old workflow is marked "No new instances" after you associate the new version (this should be automatic)
- Based on this, it seems like workflows should be in their own features to allow for simpler versioning
- Delay activities were not working correctly
- Worked with MS Premier support for weeks to try and resolve this
- Resetting the SharePoint timer service made the delay activity work correctly
- When they are initialized in the designer, setting the value to something else in the initialize event does not work
- If you bind the timeout property to a dependency field then it can be set in the initialize timeout handler, much like SendEmailActivity properties
Wednesday, November 26, 2008
ReSharper Fails to Load with a Generic Error
After installing Resharper 4.1 on Windows 2003 server, I got this message when I start Visual Studio 2008:
The Add-in 'JetBrains ReSharper 4.1' failed to load or caused an exception.
Error Message: The system cannot find the file specified.
Error Number: 80070002
I emailed JetBrains support and got a quick response. It turns out my system did not have Extensibility.dll installed in the GAC (Global Assembly Cache), and ReSharper requires this dll to run.
To figure this out, I opened explorer to the path "%windir%\assembly", which brings up a shell extension that displays the contents of your GAC.
The fix was easy, I just had to find the dll, which is installed with Visual Studio. Once I knew the location, I opened the Visual Studio command prompt and ran
gacutil /i "C:\Program Files\Common Files\Microsoft Shared\MSENV\PublicAssemblies"
Hopefully this saves someone else some time.
Thursday, September 4, 2008
Making the ASP.NET DataGrid Usable
If you have ever used an ASP.NET DataGrid (or GridView without a DataSource control), then you know there is a lot of boiler plate code needed. The project I am working on right now has a bunch of grids that need to edit, add and page over a certain data type, so I we wanted to avoid duplicating all that code across every page.
A little research and a prototype later, I came up with what I call the AutoGrid. For the most part, it just handles the editing, paging and error handling internally. Arguably more interesting is the fact that it is a generic control; I was under the impression that web controls could not have type parameters. While this is technically true, there is a cool work around, which is detailed in a blog post by Eilon Lipton.
Basically, you need create a control with generic arguments, and whatever functionality you are after. Then to create another control to second control to be used in asp.net markup. It needs to inherits from the first control and have a string property for each generic argument. Finally you override the ControlBuilder for your first control and construct your second control by passing the type arguments to a Type instance of the generic control. The code for the non-generic markup control is below:
[ControlBuilder(typeof(GenericControlBuilder))] public class GenericGrid : AutoGrid<Object> { private string _objectType; public string ObjectType { get { if (_objectType == null) { return String.Empty; } return _objectType; } set { _objectType = value; } } }
As you can see, it doesn't do much by itself. The ControlBuilder is where the magic happens (this code is straight from Eilon's example):
public class GenericControlBuilder : ControlBuilder { public override void Init(TemplateParser parser, ControlBuilder parentBuilder, Type type, string tagName, string id, IDictionary attribs) {Type newType = type; if (attribs.Contains("objecttype")) { // If objecttype is specified, create a generic type that is bound // to that argument and then hide the objecttype attribute. Type genericArg = Type.GetType( (string)attribs["objecttype"], true, true); Type genericType = typeof(AutoGrid<>); newType = genericType.MakeGenericType(genericArg); attribs.Remove("objecttype"); } base.Init(parser, parentBuilder, newType, tagName, id, attribs); } }
The other thing that makes this work is an IoC container. In this case I'm using Structure Map, but any IoC would work. The idea is that since the AutoGrid knows the type it is editing, it can ask for the appropriate data access class. Chad Myers helped me get the configuration right, using a custom TypeScanner:public class RepositoryConventionScanner : ITypeScanner { public void Process(Type type, Registry registry) { Type repoForType = GetGenericParamFor(type.BaseType, typeof(Repository<>)); if (repoForType != null) { var genType = typeof(IRepository<>).MakeGenericType(repoForType); registry .ForRequestedType(genType) .AddInstance(new ConfiguredInstance(type)); } } private static Type GetGenericParamFor(Type typeToInspect, Type genericType) { if (typeToInspect != null && typeToInspect.IsGenericType && typeToInspect.GetGenericTypeDefinition().Equals(genericType)) { return typeToInspect.GetGenericArguments()[0]; } return null; }
Then in your Application_Start event, just tell Structure Map to run this scanner on your assembly:StructureMapConfiguration .ScanAssemblies() .IncludeAssemblyContainingType<GenericGrid>() .With(new RepositoryConventionScanner());
I wrote this code for a pretty specific situation, your mileage may vary. I would love to hear thoughts about this, if you care to see the whole solution you can get it here.
Wednesday, June 25, 2008
Linq To SQL Caching
I ran into a weird behavior while trying out different usage patterns of Linq To SQL. I noticed that some queries were not hitting the database! Now I knew that Linq To SQL object tracking keeps cached copies of entities it retrieves, but my understanding was that it only used this for identity mapping and would never return stale results. After some Googling and then looking at the internals of the System.Data.Linq.Table class with Reflector, I came to the conclusion that it was indeed returning its cached results. This makes sense once you understand the way the data context works; I didn't realize the implications of object tracking. Once an object has been retrieved once by a data context, its values will not be updated by the database. This is key for the way optimistic concurrency support works in Linq to SQL, but if you are used to writing simple crud applications where you ignore concurrency it would be easy to overlook this.
On thing still puzzles me though, if I change my call from
context.Products;
to
context.Products.ToList();
I would always hit the database. It turns out that ToList calls GetEnumerator (which leads to a query being fired) whereas when I databind directly against the Table, it calls IListSource.GetList, which will return the cached table if it can. Why wouldn't you query the database to check for new objects that might have been added to your results, and why couldn't the same query use the cache when I call ToList on it?
Wednesday, June 18, 2008
Deferred Execution in Linq to SQL
Just like the last post, this one is motivated by a comment I got from someone identified as merlin981. Since we seem to have a running dialog, do you have a blog or other online presence? In any case, I wanted to explain my understanding of how Linq to SQL uses deferred execution because merlin and I seemed to have a very different ideas.
Let's take a look at a simple query like the one below.
var dbContext = new TestDataContext(); var result = from x in dbContext.Products select x;At this point, the query is just and expression tree. When you iterate over the the results, the following single query executes against the database:
SELECT [t0].[Id], [t0].[Name], [t0].[Price], [t0].[CategoryId] FROM [dbo].[Product] AS [t0]At this point, I can access the Id, Name and CategoryId of all the products that were in the the database without any other connections to the database. On the other hand, if you were to do something like this:
foreach (var product in result) { Response.Write(product.Category.Name); }
This block of code is going to hit the database once for each product. Obviously we want to avoid that, and there are several ways to do so. One is to return an anonymous type containing just the columns we need:
var result = from x in dbContext.Products select new { x.Name, CategoryName = x.Category.Name }; foreach (var product in result) { Response.Write(product.CategoryName); }This method will do an inner join and pull back just the columns we asked for. Another way is to specify load options for our original query:
var dbContext = new TestDataContext(); dbContext.LoadOptions.LoadWith<Product>(p => p.Category); var result = from x in dbContext.Products select x;
This tells the Linq to SQL Execution engine to load all the fields in the Category entity for each product. The generated SQL is below.
SELECT [t0].[Id], [t0].[Name], [t0].[Price], [t0].[CategoryId], [t1].[Name] AS [Name2] FROM [dbo].[Product] AS [t0] INNER JOIN [dbo].[Category] AS [t1] ON [t1].[Id] = [t0].[CategoryId]
I hope this has been a helpful example of how Linq To SQL uses deferred execution.
Monday, June 9, 2008
Stored Procedures, a Best Practice?
I just saw merlin981's comment on my LINQ to SQL post, thanks for taking time to leave it! That said, I think the term "Best Practice" is something of a misnomer here. There has been much written on both sides of this debate. One thing is for sure, though, a parameterized query is compiled just like a stored procedure on SQL Sever version 7.0 and on. From Frans Bouma's blog, I found this article in the SQL Server's Books Online:
SQL Server 2000 and SQL Server version 7.0 incorporate a number of changes to statement processing that extend many of the performance benefits of stored procedures to all SQL statements. SQL Server 2000 and SQL Server 7.0 do not save a partially compiled plan for stored procedures when they are created. A stored procedure is compiled at execution time, like any other Transact-SQL statement. SQL Server 2000 and SQL Server 7.0 retain execution plans for all SQL statements in the procedure cache, not just stored procedure execution plans.
So I think it is clear that sprocs will not be significantly faster than ad hoc SQL for simple cases. This is not to say that you should never use sprocs, on the contrary, there are situations where sprocs will be the only good solution (for instance, complex data manipulation that requires temporary tables). The point is that using an ORM can make development easier by allowing you to ignore the SQL for the majority of cases. If you see that parts of your application are slow, then you can fix that.
Merlin also mentioned that running queries directly against tables uses deferred execution like it is a bad thing. Deferred execution is what allows LINQ to work at all, and can improve performance in many scenarios. Of course, like any tool, it can get you into trouble if you don't understand it.