Tuesday, September 24, 2013

Format SharePoint People Picker as Hyperlink on Infopath Display list form

I wanted to display the people picker value in a list form as a hyperlink in SharePoint. By default, it was just a text field on the display form. In a normal SharePoint list form, this field can be modified to disable output escaping so that it formats as an html. I was totally unsure how to accomplish this, and pressed for time, I decided to write a script to format it as a hyperlink. If you know how to do this in InfoPath, please share!

Place this script in a content editor webpart on the display list form:

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

//make sure document is loaded first
$(document).ready(function() { 
 setTimeout(formatUserField,'1000');
});

function formatUserField()
{
 $("span[ScriptClass='CustomControl']").each(function(){
  var curValue = $(this).text();
  var newhtml = "<a href='javascript:void(0)' onclick='PopUpEmail(\"" + curValue + "\");return false;'>"+ curValue +"</a>";
  $(this).html(newhtml);
 });
}

function PopUpEmail(username)
{
    var p_recipient = username;
    var p_cc = "";
    var p_subject =  "";
    var p_body =  "";
    var objO = new ActiveXObject('Outlook.Application');     
    var objNS = objO.GetNameSpace('MAPI');     
    var mItm = objO.CreateItem(0);     
    mItm.Display();     
    mItm.To = p_recipient;
    mItm.Cc = p_cc;
    mItm.Subject = p_subject;
    mItm.Body = p_body;     
    mItm.GetInspector.WindowState = 2;
}

</script>

Tuesday, September 10, 2013

Embed image in a SharePoint outgoing email using SharePoint Designer workflow activities

If you need to embed an image into a SharePoint outgoing email:


<head>
<meta name="content-disposition" content="inline; filename=Myimage.jpg">
</head>

  • Format your email, and then to insert the image, create a div tag at the bottom of the email string:
<div style="text-align: center;">
<img src="Myimage.jpg" alt="myimagelogo" />
</div>

  • Add the action: Send Email with List Item Attachments
  • Attach the image in the activity, with the same name as used above (Myimage.jpg)
    • In mine, I always want to attach the same image in every outgoing email, so I uploaded an image to a SharePoint document library and I attached the image based on the specific title it contained
  • In the email body of the action, add the workflow variable: emailBody
  • Save, publish and test it out!


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!

Tuesday, January 22, 2013

Determine which SharePoint Web Front End Server you are hitting

Often, we need to identify which WFE server on SharePoint we are hitting when the farm has load balancing.

I decided to create a very simple feature that would display the server name by overriding the GlobalNavigation delegate control of each page that was being accessed across every site collection and sub sites in a web application.

When accessing any page, you will see the server name similar to below:




First, I created the user control, and added one simple label:

<asp:Label ID="lblServerName" runat="server" BackColor="Green" ForeColor="White"></asp:Label>

In the code behind, I put code to check whether the current user is a farm administrator, or if the query parameter, "showserver" has been passed into the url. If either are true, then I display the name of the sharepoint server on the label:

 protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (IsFarmAdmin() || ServerParamIsSet())
                {
                    this.lblServerName.Width = new Unit("100%");
                    this.lblServerName.Text = string.Format("{0}", Page.Server.MachineName);
                }
            }
            catch { }
        }



Next, I added an Elements manifest, and specified the path to where the control template will be deployed to on the server, a sequence number lower than 100, and specified it to place the user control in the GlobalNavigation delegate control placeholder.

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <Control Id="GlobalNavigation" Sequence="90" ControlSrc="~/_ControlTemplates/SPServerName/SPServerName.ascx" />
</Elements>


Finally, I created a feature and set it to deploy to the web application.


Installation instructions:
1. Download the wsp I uploaded to http://spservername.codeplex.com/
2. Deploy the solution to your farm
3. Go to the web application features, and enable the SPServerName feature


Notes:
  • There are no changes made to any master page and the feature can easily be turned on and off
  • By default, Farm administrators will always see the server name at the top of the page.
  • Non-farm administrators can also display the server name if they pass in the query parameter "showserver=1"
Ex:

http://myportal.com/siteabc?showserver=1