3

I have created calendar list using visual studio 2013 with two views in List Instance. Now i want to create some calendar overlays using same views i have created in list instance.

Please help me to create calendar overlay using visual studio list instance.

I can achieve through SharePoint UI but i need solution to achieve the same using VS list instance.

3
  • it is not possible Commented Jul 8, 2015 at 10:58
  • it is possible using SharePoint UI So there must be any way to achieve this. Commented Jul 8, 2015 at 10:59
  • 1
    yes, you can do it with UI but cant do it with Instance Commented Jul 8, 2015 at 13:12

1 Answer 1

2

What does OP mean by instance? List definition in XML? In that case maybe not, but there is code for that which I have successfully used many a time. Just stick that in an event receiver and you're good to go:

public static void AddCalendarOverlay(SPList targetList, string viewName, SPList overlayList, string overlayListView, string overlayName, string overlayDescription, CalendarOverlayColor color, bool alwaysShow, bool clearExisting)
{
    bool sharePoint = overlayList != null;

    SPView overlayView = overlayList.DefaultView;
    if (!string.IsNullOrEmpty(overlayListView))
    {
        if (overlayList.Views[overlayListView] != null)
        {
            overlayView = overlayList.Views[overlayListView];
        }
    }

    string linkUrl = overlayView.ServerRelativeUrl;

    SPView targetView = targetList.DefaultView;
    if (!string.IsNullOrEmpty(viewName))
    {
        if (targetList.Views[viewName] != null)
        {
            targetView = targetList.Views[viewName];
        }
    }

    XmlDocument xml = new XmlDocument();
    XmlElement aggregationElement = null;
    int count = 0;
    if (string.IsNullOrEmpty(targetView.CalendarSettings) || clearExisting)
    {
        xml.AppendChild(xml.CreateElement("AggregationCalendars"));
        aggregationElement = xml.CreateElement("AggregationCalendar");
        xml.DocumentElement.AppendChild(aggregationElement);
    }
    else
    {
        xml.LoadXml(targetView.CalendarSettings);
        XmlNodeList calendars = xml.SelectNodes("/AggregationCalendars/AggregationCalendar");
        if (calendars != null)
            count = calendars.Count;
        aggregationElement = xml.SelectSingleNode(string.Format("/AggregationCalendars/AggregationCalendar[@CalendarUrl='{0}']", linkUrl)) as XmlElement;
        if (aggregationElement == null)
        {
            if (count >= 10)
                throw new SPException(string.Format("10 calendar ovarlays already exist for the calendar {0}.", targetList.RootFolder.ServerRelativeUrl));
            aggregationElement = xml.CreateElement("AggregationCalendar");
            xml.DocumentElement.AppendChild(aggregationElement);
        }
    }
    if (!aggregationElement.HasAttribute("Id"))
        aggregationElement.SetAttribute("Id", Guid.NewGuid().ToString("B", CultureInfo.InvariantCulture));

    aggregationElement.SetAttribute("Type", sharePoint ? "SharePoint" : "Exchange");
    aggregationElement.SetAttribute("Name", !string.IsNullOrEmpty(overlayName) ? overlayName : (overlayList == null ? "" : overlayList.Title));
    aggregationElement.SetAttribute("Description", !string.IsNullOrEmpty(overlayDescription) ? overlayDescription : (overlayList == null ? "" : overlayList.Description));
    aggregationElement.SetAttribute("Color", ((int)color).ToString());
    aggregationElement.SetAttribute("AlwaysShow", alwaysShow.ToString());
    aggregationElement.SetAttribute("CalendarUrl", linkUrl);

    XmlElement settingsElement = aggregationElement.SelectSingleNode("./Settings") as XmlElement;
    if (settingsElement == null)
    {
        settingsElement = xml.CreateElement("Settings");
        aggregationElement.AppendChild(settingsElement);
    }
    if (sharePoint)
    {
        settingsElement.SetAttribute("WebUrl", overlayList.ParentWeb.Site.MakeFullUrl(overlayList.ParentWebUrl));
        settingsElement.SetAttribute("ListId", overlayList.ID.ToString("B", CultureInfo.InvariantCulture));
        settingsElement.SetAttribute("ViewId", overlayView.ID.ToString("B", CultureInfo.InvariantCulture));
        settingsElement.SetAttribute("ListFormUrl", overlayList.Forms[PAGETYPE.PAGE_DISPLAYFORM].ServerRelativeUrl);
    }

    targetView.CalendarSettings = xml.OuterXml;
    targetView.Update();
}

Most of the code is self-explanatory, but if you need any clarification I will be happy to help.

3
  • Thanks for reply & your efforts but i had completed it long time back....:) Commented Feb 19, 2016 at 10:24
  • Still, some users might find it useful :) Commented Feb 19, 2016 at 10:26
  • Yes you are right.. that's why i appreciated your effort by up-vote the answer. as i know we can do it by this way well. Thanks Again Man..:) Commented Feb 19, 2016 at 10:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.