Custom SPCalendarItems in SPCalendarView SharePoint 2010?

Answered Custom SPCalendarItems in SPCalendarView SharePoint 2010?

  • martedì 14 settembre 2010 23:58
     
      Contiene codice
    Hello, in SharePoint 2007 we could use the SPCalendarView control (Microsoft.SharePoint.WebControls) to display custom created calendar events (for example from a separate SQL table).

     

    Most links I have seen out there seem to point back to this post: http://www.tonstegeman.com/Blog/Lists/Posts/Post.aspx?List=70640fe5-28d9-464f-b1c9-91e07c8f7e47&ID=60 In summary, things looked like this:

     

    private SPCalendarView _view;
    
    protected override void CreateChildControls()
    {
        base.CreateChildControls();
    
    
        _view = new SPCalendarView();
        _view.EnableViewState = true;
    
        _view.DataSource = GetCalendarItems();
    
        DataBind();
    
    
        Controls.Add(_view);
    }
    

     

     

     

    private SPCalendarItemCollection GetCalendarItems()
    {
        SPCalendarItemCollection items = new SPCalendarItemCollection();
    
        SPCalendarItem item = new SPCalendarItem();
        item.StartDate = DateTime.Now;
        item.EndDate = DateTime.Now.AddHours(1);
        item.hasEndDate = true;
        item.Title = "First calendar item";
        item.DisplayFormUrl = "/myurl";
        item.Description = "This is a test";
        item.IsAllDayEvent = false;
        item.IsRecurrence = false;
    
        items.Add(item);
    
        return items;
    }
    

     

    A direct port of this doesn't work in 2010. In order for it to work, you need to add the following in CreateChildControls:

     

    _view.EnableV4Rendering = false;
    

     

    This is OK, but you don't get all the visual styling of 2010 (aka v4 styles). How can I get the v4 styling by creating custom SPCalendarItem objects?


    Brian McCullough

Tutte le risposte

  • giovedì 16 settembre 2010 07:28
     
     

    Hi brianpmccullough,

     

    Thanks for your post.

    Change the inherited class from “System.Web.UI.WebControls.WebParts.WebPart” to “Microsoft.SharePoint.WebPartPages.WebPart” in your web part class.

     

    Here is a similar thread.

    http://social.msdn.microsoft.com/Forums/en-US/sharepointdevelopment/thread/0925ef18-cd32-4491-9f18-e71b17b5b6a7

  • giovedì 16 settembre 2010 13:17
     
     

    This works, and in my situation, and probably most, I can change what I am inheriting from rather easily and get it to fit my application.

    But isn't the "best practice" to create custom web parts that inherit from "System.Web.UI.WebControls.WebParts.WebPart”?

    Also, what if I wanted to create this as a server and/or user control without the web part context?

    What is the Microsoft.SharePoint.WebPartPages.WebPart” adding to it to make it work?  Some javascript references?  Some CSS?  Something else? 

    Thanks much for a step in the right direction !

    -Brian


    Brian McCullough
  • venerdì 17 settembre 2010 08:36
     
     

    Hi,

     

    Glad to receive your reply.

    First, there are two ways for building web part: Asp.Net Web part and SharePoint-Based Web part.

    You can get more information about this from here :

    http://msdn.microsoft.com/en-us/library/bb153523%28office.12%29.aspx

     

    I think the best practice is based on the scenario. Every web part is suitable for some scenarios.

     

    For your last questions, may me you can get answer using the reflector tool to see the source code of the control.

    You can download it from: http://www.red-gate.com/products/reflector/

  • venerdì 17 settembre 2010 13:22
     
     Con risposta

    This guy does a pretty good high-level comparison between the 2007 and 2010 SPCalendarView implementation.  Bonus: he describes one way to color code the calendar items (think Outlook Client).

    A highlight from this post with my emphasis added:

    They broke the Calendar!

    Not really, but is sure has changed. In 2007 the calendar was created entirely server sideThe HTML sent to the browser was the final, ready to display HTML. All we needed to do was to write a little JavaScript and/or CSS to “customize” it.

    Why it’s now a bit harder to code…

    In 2010 the data for the calendar is now asynchronously loaded using a JavaScript function call after the page is loaded.

    As I suspected, it likely has to do with some Javascript library that the SharePoint Web Part is loading that the ASP.NET Web Part is loading.

    I'd still like to understand this a bit more, so my next steps, when I get some time, are to create two versions of my custom web part.  One that inherits from ASP.NET Web Part, the other from SharePoint.WebPartPages.WebPart.  Put them on two different pages, and analyze the rendered markup looking for differences.


    Brian McCullough
    • Contrassegnato come risposta Wayne Fan giovedì 7 ottobre 2010 06:53
    •  
  • sabato 14 aprile 2012 14:36
     
     

    Don't have to Disable V4 Rendering, Just install this Dec2011 CU : http://support.microsoft.com/kb/2596998

    , will solve all SPCalendarView problem in Sharepoint2010


  • lunedì 28 maggio 2012 08:51
     
     

    I have intalle the hotfix firstly the calandarview it work but after it don’t work

    when i use the calView.EnableV4Rendering = false the calandar work properly in wss3.0 interface