Tuesday, October 22, 2013

SharePoint Add Calendar Overlay Programmatically

This is sample of code to add calendar overlay pro grammatically:

using (SPSite site = new SPSite("http://sp2013-tarek294:8000/"))
{
     using (SPWeb web = site.OpenWeb())
    {
          web.AllowUnsafeUpdates = true;
          SPList list = web.Lists["Calendar"];
          SPView m_view = list.Views["Calendar"];
          m_view.CalendarSettings = SerializeAccessors();
          m_view.Update();
          web.AllowUnsafeUpdates = false;

    }
}

private static string SerializeAccessors()
{
 return "<AggregationCalendars>
  <AggregationCalendar Id="{56594c0c-5649-458e-a4e3-939589ddc75d}" Type="SharePoint" Name="Holiday" Description="Holiday" Color="5" AlwaysShow="True" CalendarUrl="/Lists/Calendar/Holiday.aspx">
    <Settings WebUrl="http://sp2013-tarek294:8000" ListId="{6913f374-1a4e-4772-8c5f-9f5d56610f71}" ViewId="{0091da5e-e5a4-4e1f-ab96-19f96eeb59fa}" ListFormUrl="/Lists/Calendar/DispForm.aspx" />
  </AggregationCalendar>
  <AggregationCalendar Id="{d65114d1-cb97-41db-a7eb-7cc3614c957e}" Type="SharePoint" Name="Meeting" Description="Meeting" Color="8" AlwaysShow="True" CalendarUrl="/Lists/Calendar/Meeting.aspx">
    <Settings WebUrl="http://sp2013-tarek294:8000" ListId="{6913f374-1a4e-4772-8c5f-9f5d56610f71}" ViewId="{68513c26-b132-4d9e-b9ca-acc74a4cf1dc}" ListFormUrl="/Lists/Calendar/DispForm.aspx" />
  </AggregationCalendar>
  -->
  <AggregationCalendar Id="{cd33c105-6dd4-4cb9-b6ee-ca26dfcdb70e}" Type="SharePoint" Name="TestView" Description="TestView" Color="3" AlwaysShow="True" CalendarUrl="/Lists/Calendar/TestView.aspx">
    <Settings WebUrl="http://sp2013-tarek294:8000" ListId="{6913f374-1a4e-4772-8c5f-9f5d56610f71}" ViewId="{2D81DDC1-A360-4585-BF4A-7C0E512C7AA8}" ListFormUrl="/Lists/Calendar/DispForm.aspx" />
  </AggregationCalendar>
</AggregationCalendars>"
}

SharePoint 2013 Read Overlay setting using javascript



You can use the following the code to retrieve Calendar Overlay settings using Javascript:

EnsureScriptFunc('sp.js', 'SP.ClientContext', function(){
            var clientContext = new SP.ClientContext.get_current();
            if (clientContext != undefined && clientContext != null) {
                 var web = clientContext.get_web();
                                                                                                                               
                 var listCollection = web.get_lists();
                 var list = listCollection.getByTitle("Calendar");
                 var viewCollection = list.get_views();
                 this.view = viewCollection.getByTitle("Calendar");
                 clientContext.load(this.view);
                 clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded),    Function.createDelegate(this, this.onQueryFailed));
}
});
 function onQuerySucceeded() {
        alert(this.view.get_htmlSchemaXml()); //this contains overlays settings
 }
                               
 function onQueryFailed(sender, args) {
       alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
 }

Monday, October 21, 2013

Remove “recent” links from the Left Nav in SharePoint 2013

You can use the following script to master page to remove recent links from Quick launch in SharePoint 2013
jQuery(document).ready(function() {
 jQuery(".ms-core-listMenu-item:contains('Recent')").parent().hide();
});

Friday, October 11, 2013

SharePoint Impersonation mistake

SharePoint API give us function  SPSecurity.RunWithElevatedPrivileges to impersonate by system account users. Some users make mistake in using it by using current site instead of initiate new SPSite.:

SPSecurity.RunWithElevatedPrivileges(() =>
{

using ( SPSite site = SPContext.Current.Site)
{
using (SPWeb web = site.OpenWeb())
{
....................
}
}
}
});

Developer need to use:
SPSite site = new SPSite(SPContext.Current.Site.Url)

instead of:
SPSite site = SPContext.Current.Site

Code will be:
SPSecurity.RunWithElevatedPrivileges(() =>
{

using ( SPSite site = new SPSite(SPContext.Current.Site.Url) )
{
using (SPWeb web = site.OpenWeb())
{
....................
}
}
}
});