Wednesday, October 24, 2012

Image load Tracking in Google Analytics


Image tracking is provided by default with GA, to achieve this we can use Event Tracking for images.


Example:
So if you have an image “Mritunjay.jpg”

<img src="images/Mritunjay.jpg" />
<img src="images/Mritunjay.jpg" onload="_gaq.push(['_trackEvent','Image Tracking','Mritunjay']);" />


‘Image Tracking’ represents the Category
‘Mritunjay’ represents the Action

Thursday, October 18, 2012

Code Blocks are not allowed in this file - Performance Point part 2

As discussed in the previous post how we can fix the Code block error for performance Point, now lets find the reason why the error occurs on the PPSample.aspx and how we can eliminate it at the time of writing the Feature for Masterpage.

The main reason for this error is the the page is updated with the property "customized" this can be checked by the file property "CustomizedPageStatus" in the document library.


following is the code to check if the file is customized/unghosted.

void WriteCustomizedAndNone(object sender, EventArgs e)
        {
            string myResult = '';
            string myListName = txt1.Text;
            SPList myList = SPContext.Current.Web.Site.RootWeb.Lists[myListName];
            if (!Directory.Exists('C:\\Unghosted Files\\'))
            {
                Directory.CreateDirectory('C:\\Unghosted Files\\');
            }
            myResult += 'Status of items<br><br>';
            myResult += '<table style='border:solid 1px gray;'>';
            myResult += '<tr>' + '<td>' + 'Name' + '</td>' + '<td>' + 'Setup Path' + '</td>' + '<td>' + 'is customized' + '</td>' + '</tr>';
            foreach (SPListItem anItem in myList.Items)
            {
                myResult += '<tr>' + '<td >' + anItem.Name + '</td>' + '<td >' + anItem.File.Properties['vti_setuppath'] + '</td>'  + '<td>' + anItem.File.CustomizedPageStatus + '</td>' + '</tr>';
                if (anItem.File.CustomizedPageStatus.ToString() != 'Uncustomized')
                {
                    byte[] myBytes = anItem.File.OpenBinary();
                    System.IO.File.WriteAllBytes('c:\\Unghosted Files\\' + anItem.File.Name, myBytes);
                }
            }
            myResult += '</table>';
            result.Text = myResult;
        }/*Thanks to my friend Siva for this code*/


Note: The page gets updated with the property with Customized if there is any update happens on the page after/during the feature activation, make sure there is no changes happening during the feature activation. 

Any change in the master page will result in UnGhosting which will update the file property to "Customized" and will result in the issue with Performance point "Code Blocks are not allowed in this file".




Wednesday, October 3, 2012

Code Blocks are not allowed in this file - Performance Point



After the customization of the master page in SharePoint 2010, we sometimes get the following error
"Code Blocks are not allowed in this file"

There are two solution for this
1. Add the Page Parser Path in web.config
<PageParserPaths>
       <PageParserPath VirtualPath="/BICenter/Pages/ppssample.aspx" CompilationMode="Always" AllowServerSideScript="true" />
     </PageParserPaths>

2. Modify the PerformancePoint site template by updating the /BICenter/Pages/ppssample.aspx

Replace the following
var siteCollection = "<%=SPHttpUtility.UrlPathEncode(siteCol.Value,false,true)%>";
var siteLocation = "<%=SPHttpUtility.UrlPathEncode(site.Value,false,true)%>";

with
var siteCollection = "/";
var siteLocation = "/BICenter/";

Monday, September 24, 2012

Flyout control for Win8 Metro App

A flyout control is a popup control appear on an event.

The code to create flyout pop-up from Settings Page



args.Request.ApplicationCommands.Add(
new SettingsCommand("settings", "Application Settings",
    a =>
    {
     int width = 500;
     _settingsPopup = new Popup();
     _settingsPopup.Closed += OnPopupClosed;
     Window.Current.Activated += OnWindowActivated;
     _settingsPopup.IsLightDismissEnabled = true;
     _settingsPopup.Width = width;
     _settingsPopup.Height = Window.Current.Bounds.Height;
     AppSettingsFlyoutPopup mypane = new AppSettingsFlyoutPopup();
     mypane.Width = width;
     mypane.Height = Window.Current.Bounds.Height;
     _settingsPopup.Child = mypane;
     _settingsPopup.SetValue(Canvas.LeftProperty, Window.Current.Bounds.Width - mypane.Width);
     _settingsPopup.SetValue(Canvas.TopProperty, 0);
     _settingsPopup.IsOpen = true;
     this.layout.Opacity = .3;
     _settingsPopup.Closed += (e, s) => this.layout.Opacity = 1;
    }));

 
 

Wednesday, September 5, 2012

Feature Stapling in SharePoint 2010


Feature stapling helps us to enable the feature at following scope
1. Web
2. Site
3. Web application
4. Farm

Stapling required atleast two features
1. Feature that needs to be stapled
2. Feature to staple at different scope.
Obviously the stapler scope should be above the feature being stapled.

To activate the feature for type of sites in web application, we need to associate stapling a feature to global site definition(TemplateName=" GLOBAL") and blank site (TemplateName=" STS#1").

Elements file of Stapler feature will look like

<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
<FeatureSiteTemplateAssociation   Id="c4386b61-3aa0-4017-98c4-0feb17a97cb5" TemplateName=" GLOBAL" />
<FeatureSiteTemplateAssociation   Id="c4386b61-3aa0-4017-98c4-0feb17a97cb5" TemplateName=" STS#1" />
</Elements>



Solution example


Dialog box Styling in SharePoint 2010

SharePoint 2010 has two important classes to control the dialog display
.s4-notdlg
.ms-dialog


s4-notdlg
Apply the class to any div which is not required to disply

ms-dialog
This class is added to the tag of dialog, so we can modify the css of any class in the dialog.
e.g.
.ms-dialog .ms-cui-topBar2
{
   height:0px;
}

Friday, August 10, 2012

allowunsafeupdates, is really to safe



Using AllowUnsafeUpdates can be done in 2 ways
1. Get request :
2. Post request

Get request
It opens the gateway for the cross-site scripting, if we cannot switch to POST request then in that case we can go ahead with allowunsafeupdates, but make sure the property is set to false. Even though the property automatically sets to false after the use, but in case of exception we might end up in a situateion the property is true.


Eg. on how to use allowunsafeupdates

using (SPSite spsite = new SPSite(SPContext.Current.Site))
{
using (SPWeb spWeb = spsite.OpenWeb())
{
 try
 {
  SPFolder spfolder = spWeb.Folders[spWeb.Url + "/LibName/"];
  byte[] content = System.Text.Encoding.UTF8.GetBytes(strcontent);
  string filenname = "Upload" + DateTime.Now.ToString() + ".html";
  spWeb.AllowUnsafeUpdates = true;
  SPFile spfile = spfolder.Files.Add(filenname, content, true);
 }
 catch(Exception exp)
 { throw new exception(exp.message()) }
 finally
 {   spWeb.AllowUnsafeUpdates = false;  }
 }
}


Post request
In case of POST request we should go with SPUtility.ValidateFormDigest(), but this uses the digest value which can expire and will give the security validation exception.
Eg. on how to use allowunsafeupdates

using (SPSite spsite = new SPSite(SPContext.Current.Site))
{
using (SPWeb spWeb = spsite.OpenWeb())
{
 try
 {
  SPFolder spfolder = spWeb.Folders[spWeb.Url + "/LibName/"];
  byte[] content = System.Text.Encoding.UTF8.GetBytes(strcontent);
  string filenname = "Upload" + DateTime.Now.ToString() + ".html";
  SPUtility.ValidateFormDigest();
  SPFile spfile = spfolder.Files.Add(filenname, content, true);
 }
 catch(Exception exp)
 { throw new exception(exp.message()) }
}



Reference : http://hristopavlov.wordpress.com/2008/05/16/what-you-need-to-know-about-allowunsafeupdates/