I thought this was an easy task – I was wrong. I mean, Microsoft makes it sound like an easy task. I even found it on one of their official SharePoint 2013 training modules. But, they have it wrong. Yes, you heard me right, the official training is not correct. Well, I might be a little harsh. What they say works, but it causes unexpected side affects.
Before I get too far ahead of myself, let me digress a little. First off, it’s getting quite frustrating to read articles from Microsoft or other Bloggers on SharePoint 2013 Apps because they rarely tell us which app model (Auto, SharePoint or Provider hosted) they are referring too. So, let me be clear, this post is only intended for Auto or SharePoint hosted apps. So far, I do not see a way to add List Views to Provider hosted Apps. There is a real good reason for it. The Provider hosted model does not have the SharePoint dlls on the server you are hosting the code on (well, that’s typical, I guess you could import them over if you wanted to or use a SharePoint box as your provider). So, the ASP.Net references I am going to mention (i.e.: the assemblies/dll references for the List View) don’t actually exist on your provider (typically).
Ok, lets start then. My requirement is pretty simple. Add the Workflow Task List to a page in my SharePoint App and default it to the “My Items” view.
Microsoft’s recommendation for adding a List View to a SharePoint App. Just add the following to your page:
<WebPartPages:WebPartZone runat="server" FrameType="None" ID="TaskListZone" >
Easy enough. I did it. It seemed to work. Well, did it now??? I went back to my app a few weeks later and notice multiple WebParts in the zone. How did this happen? I only put one WebPart in the Zone. Well, it turns out, every time I updated my app it added another WebPart to the zone. That’s a little frustrating. I am sure it is just a setting somewhere. But, I have an App to build, I don’t have time to track down why this is happening now.
So, my next requirement is to show a particular view(the MyItems view). So, I went to the XSLTListViewWebPart documentation to see which property I need to set to define the actual “view”. Man, that documentation is difficult to follow. I spent a while on this and figured I might be barking up the wrong tree.
So, I took a step back. You know what – I never did it this way in SharePoint 2010 or when building full trust code. I always deployed WebParts in modules. The only reason I was even doing it this was is because I saw it in the SharePoint 2013 App trainings from Microsoft. Could it be possible those trainings weren’t showing me the best practice? No, that can’t be. So, I decided to do this how I believe best practices are with SharePoint – in the elements.xml file that deploys the page.
So, first I added the WebPart Zone in the page with no XSLTListViewWebPart in it:
<WebPartPages:WebPartZone runat="server" FrameType="None" ID="TaskListZone" />
Next I went to my elements.xml that deploys my actual webpage to the app. Then I simply added the View to the appropriate WebZone in the elements.xml. Thus, when the page gets deployed the WebPart gets added to the correct zone:
<File Path="Pages\Tasks.aspx" Url="Pages/Tasks.aspx" ReplaceContent="TRUE">
<View List="Lists/WorkflowTaskList" BaseViewID="5" WebPartZoneID="TaskListZone" WebPartOrder="1">
<type name="Microsoft.SharePoint.WebPartPages.XsltListViewWebPart,Microsoft.SharePoint,Version=22.214.171.124,Culture=neutral,PublicKeyToken=71e9bce111e9429c" />
<importErrorMessage>Cannot import this Web Part.</importErrorMessage>
<property name="Title" type="string">My Tasks</property>
<property name="InplaceSearchEnabled" type="bool">False</property>
<property name="ShowToolbarWithRibbon" type="bool">False</property>
All is well with the world now. I have complete control over all my properties (event he BaseViewId to set my “MyItems” view for the list). Also, no more duplicate WebParts in the zone.
So, what is the lesson here folks – trust my experience with best practices and stop thinking everything in the SharePoint App trainings is correct. But, I still recommend the trainings from Microsoft. Mostly they’ve steered me in the right direction.