Skip to main content

Posts

Showing posts from July, 2013

Create a mailto htm with no character limit.

The HTML standard for the mailto link is 256 characters. You can get around this by loading the JavaScript redirect instead. var  body = escape(window.document.title + String.fromCharCode(13) + window.location.href + " actully  a Your long text here..." ); body = body.replace( /\//g ,  "%2F" ); body = body.replace( /\?/g ,  "%3F" ); body = body.replace( /=/g ,  "%3D" ); body = body.replace( /&/g ,  "%26" ); body = body.replace( /@/g ,  "%40" ); var  subject =  "Take a look at this!" ; window.location.href =  "mailto:?body="  + body +  "&subject="  + subject;

Get Sum of Columns in a SharePoint list (Threshold edition)

It is a known fact that once the treshold limit is reached in SP, everything seems frozen.  At this point you have some options. Increase the limit using powershell or from the Central Admin OR Index the columns that you will like to operate on and create views based on those. However programmatically if you want to sum a list that has reached its threshold.  You can do so in batches as hown below:          protected   void  GetSummary( out   long  ideas,  out   long  votes,  out   long  comments,  out   long  transform)         {              long  ideasum = 0;              long columntoSum = 0;              long  commentS...

Get the latest ID from a SharePoint list

  public   int MaximumID()         {              SPQuery  query =  new   SPQuery ();             query.Query =  " " ;             query.RowLimit = 1;              SPListItemCollection  items =  SPContext .Current.Site.RootWeb.Lists[ "ListName" ].GetItems(query);              int  maxId =  Convert .ToInt32(items[0][ "ID" ]);              return  maxId;         }

Create a jQuery alert programmatically

From the code behind:   Assuming you have jquery on your page already.   StringBuilder  strScriptFeatured =  new   StringBuilder (); strScriptFeatured.Append( "$(document).ready(function(){" ); strScriptFeatured.Append( "alert('Your alert here!');" ); strScriptFeatured.Append( "});" ); Page.ClientScript.RegisterStartupScript( this .GetType(),  "Script" , strScriptFeatured.ToString(),  true );   QED  

Create a random item in a list

To create a random item in a SharePoint list:     private   static   SPListItem  GetRandomItem( SPListItemCollection  list) { var  theIDs =  new   List < int >(); foreach  ( SPListItem  item  in  list) { theIDs.Add(item.ID); } theIDs.Shuffle(); Int32 val = theIDs.FirstOrDefault(); SPListItem  randomItem = list.GetItemById(val); return  randomItem; }   The randomization is done here:           public   static   void  Shuffle ( this   IList  list)         {              var  randomNumber =  new   Random (DateTime.Now.Millisecond);              var  n = list.Count;   ...

Remove chrome on a web part

The web part properties are many and one of the OOB property is the chrome that displays the title.  To remove the chrome programmatically. You can on instantiation or page load do this. this .ChromeType =  PartChromeType .None;    QED

Page Redirect in a sandbox

To redirect in a SharePoint sandbox is a nightmare. But don't panic, there is a way out. Create a literal control in your html as shown below:   < asp : Literal   ID = "litRedirect"   runat = "server"   /> and  in the codebehind do this:   litRedirect.Text =    string .Format( " " ,  string .Format( "{0}?k={1}" , thesearchresult, txtSearch.Value.Trim()));

Set Search Text Box Default

Ways to set search text box default is given below:   txtSearch.Value  =  "Search keywords" ;   txtSearch.Attributes.Add( "onfocus" ,  "if (this.value == 'Search keywords') this.value = '';" );   txtSearch.Attributes.Add( "onBlur" ,  "if (this.value == '') this.value = 'Search keywords';" );   this .Page.Form.DefaultButton = imgSearch.UniqueID;   Alternatively, you could wrap a panel control around your tags and set the default control on  that Panel.     < asp : Panel   ID = "pnlsearch"   runat = "server"   DefaultButton = "imgSearch" >                      < div   id = "search-drop" >                   ...

Elapsed Time Generator

     public   class   ElapsedTimeGenerator     {          public   string  Generate(DateTime dateTime)         {             TimeSpan elapsedTime = DateTime.Now.Subtract(dateTime);              if  (elapsedTime.Days > 730)             {                  double  years = elapsedTime.Days / 365;                  return   String .Format( "More than {0} years ago" ,  Math .Floor(years).ToString());      ...