Friday, May 3, 2013

Filter SharePoint list with Partial Postback to page

The other day, I wanted to filter a list view web part, triggered from a drop down list on my page. I couldn't connect to a standard out of the box filter web part because the dropdown values were populated with values retrieved from a web service. Every time I select a new value from the drop down, I wanted to automatically filter the sharepoint list with that value, without doing a full postback and refreshing the page (similar to the effect of when you filter a list from the list column options).

To start things off:
  • I created a document library and created a new column called "DocType" with a few choices:
    • Letter
    • Memo
    • Email
  • I made sure the DocType column appeared on my default view (this will be the view I use to do my filtering later)
  • I uploaded a few documents into my library, and provided the DocType for each
  • On my site page, I added the list view web part for this Document Library
  • From the AJAX Options under the list view web part settings:
    • Check off Enable Asynchronous Update
    • Show Manual Refresh Button
  • Using IE developer tools, find the element for the refresh icon with the id of ManualRefresh


Copy the outer anchor tag's onclick event (this event triggers a partial post back to the page)

  • Add a form web part to the same page
  • Grab the ID in the __doPostBack call and replace it in this script, and put the script into the form web part and save.
<div style="width:400px; height:20px; margin:0px auto; padding-bottom:20px; font-family:Verdana,Arial,Helvetica,Sans-serif;font-size:10pt;">
 Select Doc Type: 
 <select id="ddlDocType">
  <option></option>
  <option>Letter</option>
  <option>Memo</option>
 </select>
</div>


<script type='text/javascript' src='/assets/js/jquery-1.7.2.min.js'></script>
<script type="text/javascript"> 

$(document).ready(function(){
 //on the dropdown change event, call FilterMyList
 $("#ddlDocType").change(function(){
  FilterMyList();;
 });
});


function FilterMyList()
{
//get the selected value of the drop down
 var selectedDocType = $("#ddlDocType").val();
 if(selectedDocType == "" )
 { 
 //clears the filter
  __doPostBack("ctl00$m$g_95403266_84ab_485e_be73_8857b5d90f63$ctl02", "NotUTF8;__filter={DocType=" + "##dvt_all##"  + "}");
  return;
 }
 //filters the list on the selected docType
 __doPostBack("ctl00$m$g_95403266_84ab_485e_be73_8857b5d90f63$ctl02", "NotUTF8;__filter={DocType=" + selectedDocType + "}");
}  
</script>


Now, when you select a value from the dropdown, it should automatically filter the list without a full postback
 

Wednesday, May 1, 2013

SharePoint Site Actions Menu: Hide Sync to Workspace and Edit in SharePoint Designer

In my environment, I do not want to show users the Site Actions menu options that allow them to sync to workspace or click on Edit in SharePoint Designer.

I created this script that would iterate the menu options and hide the ones I didnt want available


ExecuteOrDelayUntilScriptLoaded(HideSiteActionMenuItems, "SP.Ribbon.js");


function HideSiteActionMenuItems()
{
 var menuItems = document.getElementsByTagName("ie:menuitem");
 if( menuItems != null )
 {
  for( var i = 0; i < menuItems.length; i++ )
  {
   var keyId = menuItems[i].id;
   if(keyId.endsWith("MenuItem_TakeOffline"))//Hide Sync to SharePoint Workspace
    menuItems[keyId ].hidden = true;
   else if( keyId.endsWith("MenuItem_EditSite")) //Hide Edit in SharePoint Designer
    menuItems[keyId].hidden = true;  
  }
 }
}

 



SharePoint Calendar: Expand all events on load

This is my approach to expand all calendar events on load, but if you have a better way, please share!

On the calendar page, add a reference to jQuery and also place a style tag to hide the "expand/collapse" links on load:

<style>
 .ms-cal-nav{display:none;}
</style>


Hook into the existing SharePoint calendar load function:

_spBodyOnLoadFunctionNames.push('changeCalendarEventLinkIntercept');

function changeCalendarEventLinkIntercept()
{
  var OldCalendarNotify4a = SP.UI.ApplicationPages.CalendarNotify.$4b;
  SP.UI.ApplicationPages.CalendarNotify.$4b = function () 
    {
      OldCalendarNotify4a();
      changeCalendarEventLinks();
    }
}


In the function call, find all anchor tags with the attribute evtid marked as 'expand_collapse' and click on the hyperlink:

function changeCalendarEventLinks()
{
//expand all
 $("a[evtid='expand_collapse']").each(function(){
  $(this)[0].click();
 });
}

SharePoint Calendar Overlay - Color code documents and display direct links to document library

I wanted to have a color coded view of documents in a SharePoint calendar, that would be populated from a document library. Also, the link on each item in the calendar would point directly to the document itself. We will also want to make sure that all documents for that date are expanded by default.


End Result: (on hover, you'll see that the hyperlink has changed to the direct link to the document)



 
  1. In a document library, create a column that you want to categorize your documents. I chose to create a choice field called "Document Type" and gave it 3 choices:
    1. RFP
    2. Proposal
    3. Contract
  2. In the same library, create a date column that you will use to show the documents in a calendar. I chose to create a date field called "Due Date"
  3. Upload your documents, and provide the Document Type and the Due Date for each.




  1. In the libray, create a Calendar view for each particular Document Type
    1. For the name of the view, input the document type
    2. For the Begin and End date, select the Due Date field
    3. For the month view, week view and day view titles, select the Document Type field from the drop down.
    4. Filter this view on the desired Document Type


  1. Now that all the views have been created, go to the Calendar (a team site should already have one, if it doesn't, create one).
  2. On the Calendar, select the Calendar tab from the ribbon and click on the Calendars Overlay
  3. From there, click New Calendar
    1. Give the calendar the name of the Document Type
    2. Select a color
    3. From the list drop down, select the document library that you configured earlier
    4. Select the Calendar view corresponding to the Document Type
    5. Repeat for each document type

Now all the documents will appear on the calendar, color coded based on Document Type and displayed based on Due Date

Next up: We will need to create a script, that we will use to override the CalendarNotify so that we can manipulate the hyperlinks on our Calendar to point directly to the document, instead of the list form
  1. Create a javascript file and store it in your assets library
    1. Add a reference to jQuery and SPServices
Add the following script

<style>

/* hide the collapse/expand on load (we will make it expand in script) */
.ms-cal-nav{display:none;}

/*hide the time */
.ms-acal-time {
 DISPLAY: none
}

.ms-acal-sdiv {
 MARGIN-LEFT: -58px
}

.ms-acal-sdiv A {
 POSITION: absolute; WIDTH: 100%; LEFT: 0px
}

.ms-acal-title {
 HEIGHT: 35px; PADDING-TOP: 0px
}

TABLE.ms-acal-vcont TBODY TR TD A {

 DISPLAY: none !important

}


</style>

<script type='text/javascript' src='/assets/js/jquery-1.7.2.min.js'></script>
<script type='text/javascript' src='/assets/js/jquery.SPServices-0.7.1a.min.js'></script>
<script type="text/javascript">



// load our function to the delayed load list
_spBodyOnLoadFunctionNames.push('changeCalendarEventLinkIntercept');


// hook into the existing SharePoint calendar load function
function changeCalendarEventLinkIntercept()

{
  var OldCalendarNotify4a = SP.UI.ApplicationPages.CalendarNotify.$4b;

  SP.UI.ApplicationPages.CalendarNotify.$4b = function () 
    {
      OldCalendarNotify4a();
      changeCalendarEventLinks();
    }
}

var thisSite = L_Menu_BaseUrl; //defined in SharePoint pages

function changeCalendarEventLinks()
{

 //expand all in the day
 $("a[evtid='expand_collapse']").each(function(){
  $(this)[0].click();
 });

 $(".ms-acal-sdiv").each(function(){
  var aLink = $(this).find("a");
  var href = aLink.attr("href");
  var linkSubs = href.split("ID=");
  var itemId = linkSubs[1];
  var listName = linkSubs[0].replace(thisSite +"/","");
  listName = listName.replace("/Forms/DispForm.aspx?","");
  GetListData(listName, itemId, aLink);

 });

 //necessary if date has more than 1 document
 $(".ms-acal-mdiv").each(function(){
  var aLink = $(this).find("a");
  var href = aLink.attr("href");
  var linkSubs = href.split("ID=");
  var itemId = linkSubs[1];
  var listName = linkSubs[0].replace(thisSite +"/","");
  listName = listName.replace("/Forms/DispForm.aspx?","");
  GetListData(listName, itemId, aLink);

 });

 $('td[evtid=day]').removeAttr('evtid');
 $('th[evtid=week]').removeAttr('evtid');
}



//go out and get the file name of the document, so that the link to the calendar will be a direct link to the document

function GetListData(listName, itemId, aLink){

 var linkRef = "";
 var camlFields = "<ViewFields><FieldRef Name='ID'/><FieldRef Name='FileLeafRef'/></ViewFields>";
 var camlQuery = "<Query><Where><Eq><FieldRef Name='ID'/><Value Type='Text'>" + itemId + "</Value></Eq></Where><OrderBy><FieldRef Name='ID'/></OrderBy></Query>";
 var items_Returned = null;

 $().SPServices({

  operation: "GetListItems",
  async: true, //make asynchronous so it doesn't lock up page
  listName: listName,
  listName: listName,
  CAMLQuery: camlQuery,
  CAMLViewFields: camlFields, 
  CAMLRowLimit: 1, 
  completefunc: function (xData, Status){

   items_Returned = xData;
   //alert(xData.responseText);

   var rows_Item = items_Returned.responseXML.getElementsByTagName('z:row');

   if(rows_Item.length == 0 )
   {
 //for chrome
    rows_Item = items_Returned.responseXML.getElementsByTagName('row');
   }

   for (var i = 0; i < rows_Item.length; i++)   
   {
    linkRef = rows_Item[i].getAttribute('ows_FileLeafRef');
    linkRef = linkRef.split(";#")[1];
    var newlinkRef = thisSite + "/" + listName + "/" + linkRef;

    aLink.attr("href", newlinkRef).attr("title", aLink.text());
   }
  }
 });
}

</script>





Next, we will create page that we will used to modify the rendering of the calendar and open a direct link to the document
  1. Create a page, and drop the Calendar list view web part (make sure to select the view for the Calendar you just created)
  2. Add a content editor web part to the page, and specify the link to a javascript file, which we just created
Enjoy!