Example of using SPGENView

Feb 7, 2012 at 8:29 AM

I want to provision a view for a list. On this view, I want to access the toolbar, and hide the "Add new item" button. An example of doing this programatically can be found here:

http://nikspatel.wordpress.com/2010/06/16/remove-add-new-item-from-the-sharepoint-2010-list-view-web-part/

How do you customize or replace the default view, and get access to the toolbar to hide it?

Thanks,
Trevor

Coordinator
Feb 9, 2012 at 7:54 AM
Edited Feb 9, 2012 at 7:56 AM

Hello,

Because there is no support for setting the toolbar XML in the initialize definition, you can set it on the virtual OnProvisionFinalized method if you override it. Here is an example of a task list instance with one custom view named "My tasks":

    [SPGENListInstance(WebRelURL = "Lists/ViewsExample", Title = "Views example", TemplateType = (int)SPListTemplateType.Tasks)]
   public class ViewsExampleListInstance : SPGENListInstance<ViewsExampleListInstance>
   {
       protected override void InitializeDefinition(SPGENListInstanceProperties properties)
       {
           properties.Views.Add<MyTasks>();
       }

 

      [SPGENView(UrlFileName = "MyTasks.aspx", Title = "My tasks")]
       public class MyTasks : SPGENView<MyTasks>
       {
           protected override void InitializeDefinition(SPGENViewProperties properties)
           {
               properties.Query = string.Format(
                   @"<Where><Eq><FieldRef Name=""{0}"" Lookup=""TRUE""/><Value Type=""LookupId"">{1}</Value></Eq></Where>",
                   "AssignedTo",
                   "<UserID/>");
          }

            //Provision our custom toolbar XML here since we can not specify the toolbar XML in the definition. 
           protected override void OnProvisionFinalized(SPView view, SPViewCollection viewCollection, bool updatedOnly)
           {
               view.Toolbar = string.Empty;
               view.Update();
           }
       }
  }

 

May 11, 2012 at 5:24 AM
Edited May 11, 2012 at 5:26 AM

I was over thinking it a bit.

 
protected override void OnProvisionFinalized(SPContentType contentType, bool isParentList)
{
    contentType.DisplayFormUrl = "/_layouts/MyProject/MyForm.aspx";
    contentType.Update(true);
}
and the UI is customized in my form.