none
Creating calendar view from tasks list programmatically

    Question

  • Hi All,

    I have a Tasks list of type "107" and I am trying to create a Calendar view for the tasks list. The way I created the calendar view is as follows:

                SPView calView = l.Views.Add("Calendar", l.GetView(l.DefaultView.ID).ViewFields.ToStringCollection(), 
    
    string.Empty, (uint)100, true, false, SPViewCollection.SPViewType.Calendar, false);<br/><br/>
    
    


    I would like to specify the Time interval, Calendar columns, Default scope for this view. Can anyone point me to documentation for creating a calendar view from tasks list? Do I need to specify that in the viewquery?

    Thanks.
    • Moved by Mike Walsh FIN Thursday, May 14, 2009 4:15 AM prog q (From:SharePoint - Design and Customization)
    Wednesday, May 13, 2009 8:55 PM

Answers

  • To create Calendar View Programmatically in Task List

    1.  Create Your Calendar View Via Browser.

    Here is the Trick :-) 
         
         Use RPC SharePoint URL Protocol to get the your Task List Definition Schema

         http://Server_Name/[sites/][Site_Name/]_vti_bin/owssvr.dll?Cmd=ExportList&List=GUID

         GUID == Your List GUID  ( you can obtain it via List --> Settings --> Watch out the Addressbar )

         Once you hit enter by populating above placeholders value you'll see the Schema of you List which contains all the Content Type, Fields Ref, View Information, etc..etc.. We are only concerend about Views.

         Now Locate the View Node, locate your View which you created via Browser

        Important Properties to watch out 
     
        1) View Fields
        2) View Data
        3) Query

         Copy the above data and keep it aside.


    2) Create a Console Application
     

        Write the below Code  ( Note you have to put your values of View FieldsQuery , View Data  in below code )

    using System;
    using System.Collections.Generic;
    using System.Text;
    using Microsoft.SharePoint;
    
    namespace Practice
    {
        class Program
        {
            static void Main(string[] args)
            {
                using (SPSite home = new SPSite("http://Site/SubSite"))
                {
                    using (SPWeb homeWeb = home.OpenWeb())
                    {
                        homeWeb.AllowUnsafeUpdates = true;
    
                        System.Collections.Specialized.StringCollection viewFields = new System.Collections.Specialized.StringCollection();
                        viewFields.Add("StartDate");
                        viewFields.Add("DueDate");
                        viewFields.Add("Title");
                        
    // Copy Paste your Query Value Here // string query = @"<Query> <Where> <DateRangesOverlap> <FieldRef Name=""StartDate"" /> <FieldRef Name=""DueDate"" /> <Value Type=""DateTime""> <Month /> </Value> </DateRangesOverlap> </Where> </Query>"; SPView newView = homeWeb.Lists["MyTasks"].Views.Add("New Cal", viewFields, query, 0, true, false, SPViewCollection.SPViewType.Calendar, false);

    // Copy Paste your View Data Value Here //
    newView.ViewData = @"<FieldRef Name=""Author"" Type=""CalendarMonthTitle"" /> <FieldRef Name=""AssignedTo"" Type=""CalendarWeekTitle"" /> <FieldRef Name=""AssignedTo"" Type=""CalendarDayTitle"" /> <FieldRef Name=""Title"" Type=""CalendarDayLocation"" />";



    // Note if you have selected any column which is option while creating your
    // view in browser then don't include that column --> Like below //<FieldRef Name="" Type=""CalendarWeekLocation"" /> homeWeb.AllowUnsafeUpdates = false; } } } }


        
             
    This creates a View programmatically :-)

    Thanks
    SpidyNet

       
         

    • Marked as answer by diffident Thursday, May 14, 2009 4:15 PM
    Thursday, May 14, 2009 7:27 AM

All replies

  • Hi,

    I'm not sure how to do it via code, but on the tasks list itself you can create a new view, and then on the "Create View" page choose the "Calendar View" format.

    Then you can configure your calendar view for the tasks list.

    Hopefully that helps,

    Sincerely,
    Karl
    Wednesday, May 13, 2009 11:12 PM
  • I would like to do that programmatically. I was able to do that manually.
    Wednesday, May 13, 2009 11:39 PM
  • Yes, you wrote "programmatically".

    This means it goes to the Programming forum not here !!

    Moving this to the programming forum where it should have been the whole time.

    WSS FAQ sites: http://wssv2faq.mindsharp.com and http://wssv3faq.mindsharp.com
    Total list of WSS 3.0 / MOSS 2007 Books (including foreign language) http://wssv3faq.mindsharp.com/Lists/v3%20WSS%20FAQ/V%20Books.aspx
    • Marked as answer by diffident Thursday, May 14, 2009 4:14 PM
    • Unmarked as answer by diffident Thursday, May 14, 2009 4:14 PM
    Thursday, May 14, 2009 4:14 AM
  • To create Calendar View Programmatically in Task List

    1.  Create Your Calendar View Via Browser.

    Here is the Trick :-) 
         
         Use RPC SharePoint URL Protocol to get the your Task List Definition Schema

         http://Server_Name/[sites/][Site_Name/]_vti_bin/owssvr.dll?Cmd=ExportList&List=GUID

         GUID == Your List GUID  ( you can obtain it via List --> Settings --> Watch out the Addressbar )

         Once you hit enter by populating above placeholders value you'll see the Schema of you List which contains all the Content Type, Fields Ref, View Information, etc..etc.. We are only concerend about Views.

         Now Locate the View Node, locate your View which you created via Browser

        Important Properties to watch out 
     
        1) View Fields
        2) View Data
        3) Query

         Copy the above data and keep it aside.


    2) Create a Console Application
     

        Write the below Code  ( Note you have to put your values of View FieldsQuery , View Data  in below code )

    using System;
    using System.Collections.Generic;
    using System.Text;
    using Microsoft.SharePoint;
    
    namespace Practice
    {
        class Program
        {
            static void Main(string[] args)
            {
                using (SPSite home = new SPSite("http://Site/SubSite"))
                {
                    using (SPWeb homeWeb = home.OpenWeb())
                    {
                        homeWeb.AllowUnsafeUpdates = true;
    
                        System.Collections.Specialized.StringCollection viewFields = new System.Collections.Specialized.StringCollection();
                        viewFields.Add("StartDate");
                        viewFields.Add("DueDate");
                        viewFields.Add("Title");
                        
    // Copy Paste your Query Value Here // string query = @"<Query> <Where> <DateRangesOverlap> <FieldRef Name=""StartDate"" /> <FieldRef Name=""DueDate"" /> <Value Type=""DateTime""> <Month /> </Value> </DateRangesOverlap> </Where> </Query>"; SPView newView = homeWeb.Lists["MyTasks"].Views.Add("New Cal", viewFields, query, 0, true, false, SPViewCollection.SPViewType.Calendar, false);

    // Copy Paste your View Data Value Here //
    newView.ViewData = @"<FieldRef Name=""Author"" Type=""CalendarMonthTitle"" /> <FieldRef Name=""AssignedTo"" Type=""CalendarWeekTitle"" /> <FieldRef Name=""AssignedTo"" Type=""CalendarDayTitle"" /> <FieldRef Name=""Title"" Type=""CalendarDayLocation"" />";



    // Note if you have selected any column which is option while creating your
    // view in browser then don't include that column --> Like below //<FieldRef Name="" Type=""CalendarWeekLocation"" /> homeWeb.AllowUnsafeUpdates = false; } } } }


        
             
    This creates a View programmatically :-)

    Thanks
    SpidyNet

       
         

    • Marked as answer by diffident Thursday, May 14, 2009 4:15 PM
    Thursday, May 14, 2009 7:27 AM
  • Hello,

     

    I am trying this Code, it creates a Calendar View perfectly.  But, I have a problem, I don't find how Can I change the Calendar Scope from Month to Day.

     

    Thursday, September 16, 2010 10:04 AM
  • Dude, You rock.. :)

    Some minor updates and knowledge sharing from my side (nothing much as such).  I had similar requirements and I landed onto your answer. My requirements were to create the calendar views based on tasks assigned to logged in user.

    - Make sure to start your query with "<Where>".  Specially when you have query with <And> <Or> etc.

    - Make sure to call view.Update() after you specify view.ViewData property.  This will update it back onto database.

                string query = @"<Where><And>
                          <Eq><FieldRef Name=""AssignedTo""/><Value Type=""Integer""><UserID Type=""Integer""/></Value></Eq>
                           <DateRangesOverlap>
                            <FieldRef Name=""StartDate"" />
                            <FieldRef Name=""DueDate"" />
                              <Value Type=""DateTime"">
                                <Month />
                              </Value>
                          </DateRangesOverlap>                      
                        </And></Where>";
    
                //Add the view onto views collection.
                SPView newView = list.Views.Add(_viewName, viewFields, query, 100, true, false,SPViewCollection.SPViewType.Calendar,false);
    
                //Specifing the column to be displayed in the MonthTitle, WeekTitle
                newView.ViewData = @"<FieldRef Name='Title' Type='CalendarMonthTitle' />
                          <FieldRef Name='Title' Type='CalendarWeekTitle' />
                          <FieldRef Name='Location' Type='CalendarWeekLocation' />
                          <FieldRef Name='Title' Type='CalendarDayTitle' />
                          <FieldRef Name='Location' Type='CalendarDayLocation'/>";
                newView.Update();
    

    Regards,

    Sudhir Kesharwani


    regards, Sudhir Kesharwani
    Tuesday, December 28, 2010 5:00 PM
  • Hey Anyone aware how we can change the default scope property to "Week"
    regards, Sudhir Kesharwani
    Tuesday, December 28, 2010 5:06 PM
  • I needed to set the default scope property to "Week" or "Day", too. Doesn't appear to be any way of setting this property through the object model (it's stored in embedded XML in the CalendarViewStyles node of the view schema xml). Tried taking a look at how the UI does it: unfortunately, it posts to _vti_bin/owssvr.dll. I was told to abandon the requirement and didn't follow up on exactly how to do this. You can find more information on posting to owssvr.dll on How to: Post SharePoint Foundation RPC Methods. My plan was to just simulate a post as done by the UI.
    Tuesday, February 28, 2012 7:12 PM