MOSS将数据展示成日历形式跨网站显示

需要将数据展示成日历形式,是跨网站显示,通过查看ListViewWebpart控件,发现日历是由SPCalendarView控件和SPCalendarDataSource数据源控件显示的,通过测试SPCalendarDataSource控件只能按视图进行过滤数据,如果有查询条件只有修改视图的查询条件后,才能生效。因为我显示的数据有查询条件,不采用SPCalendarDataSource控件作为数据源,用SPCalendarItemCollection类添加数据源,代码如下:

 

 

private void CreateControls()
        {
            this.pnlCalendar.Controls.Clear();

            try
            {
                if (!string.IsNullOrEmpty(this.WebTitle) && !string.IsNullOrEmpty(this.ListName))
                {

                    string strViewType = "month";
                    SPCalendarView calendarControl = new SPCalendarView();
                    calendarControl.ListName = this.ListName;      
                    string selectedDate = SPUtility.GetSelectedDate(this.Context.Request, this.Web);
                    calendarControl.SelectedDate = selectedDate;
                    calendarControl.ListName = this.ListName;
                    if (Page.Request.QueryString["CalendarPeriod"] != null)
                    {
                        switch (Page.Request.QueryString["CalendarPeriod"].ToString().ToLower())
                        {
                            case "day":
                                strViewType = "day";
                                break;
                            case "week":
                                strViewType = "week";
                                break;
                            case "timeline":
                                strViewType = "timeline";
                                break;
                            default:
                                strViewType = "month";
                                break;
                        }
                    }
                    calendarControl.ViewType = strViewType;
                    calendarControl.DataSource = this.GetItems();
                    calendarControl.DataBind();
                    this.pnlCalendar.Controls.Add(calendarControl);
                    base.CreateChildControls();
                   
                }
            }
            catch (Exception ex)
            {
                Label lblMessage = new Label();
                lblMessage.Text = ex.Message;
                this.pnlCalendar.Controls.Add(lblMessage);
            }
        }

 

 

 

public SPCalendarItemCollection GetItems()
        {
            SPCalendarItemCollection source = new SPCalendarItemCollection();

            SPQuery query = new SPQuery();
            query.Query = string.Format("<Where><And><DateRangesOverlap><FieldRef Name=\"StartDate\" /><FieldRef Name=\"EndDate\" /><Value Type=\"DateTime\"><Month /></Value></DateRangesOverlap><Eq><FieldRef Name=\"Author\" LookupId='TRUE' /><Value Type='Lookup'>{0}</Value></Eq></And></Where>", this.UserID);
            SPListItemCollection items = this.List.GetItems(query);

            string strDisplayUrl = string.Empty;

            if (this.List.Forms[PAGETYPE.PAGE_DISPLAYFORM] != null)
            {
                strDisplayUrl = this.List.Forms[PAGETYPE.PAGE_DISPLAYFORM].ServerRelativeUrl;
            }

            foreach (SPListItem item in items)
            {
                SPCalendarItem rowItem = new SPCalendarItem();
                rowItem.StartDate = Convert.ToDateTime(item["StartDate"] + string.Empty);
                rowItem.EndDate = Convert.ToDateTime(item["EndDate"] + string.Empty);
                rowItem.Title = item.Title;
                rowItem.Description = item["Description"] + string.Empty;
                rowItem.hasEndDate = true;
                rowItem.DisplayFormUrl = strDisplayUrl;
                rowItem.Location = item["Description"] + string.Empty;
                rowItem.ItemID = item.ID.ToString();
                rowItem.CalendarType = Convert.ToInt32(SPCalendarType.Gregorian);
                source.Add(rowItem);
            }
            return source;
        }

 

      这样日历显示形式就完成,如下图

 

    到此代码完成,就可以在跨站点之间显示日历视图了。

 

来源:http://www.cnblogs.com/liuyuhua/archive/2010/09/07/1820288.html

你可能感兴趣的:(OS)