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/

Getting the SMTP name for Web app


In place of explicitly hardcoding the SMTP name in the SharePoint to send mail, its always better to get the SMTP configured for the Web App, so that in future if due to any reason its getting changes, there will not be any impact on the application


private string GetSmtpServer()
{
 SPWebApplication parentWebAppp = SPContext.Current.Site.WebApplication;
 if (parentWebAppp != null)
 {
   return parentWebAppp.OutboundMailServiceInstance.Server.Address;
 }
return string.Empty;
}

Wednesday, April 11, 2012

Issue by enabling HTTPOnlyCookies on sharepoint site

HTTPOnlyCookie is enabled by default for Authentication and Sessions cookies in ASP.NET 2.0 but not for manually issued cookies. To enable it for manual issued cookie we need to add following in the web.config.
<httpCookies httpOnlyCookies="true" />

Once we enable the HTTPOnlyCookie on the SharePoint, few things like the default approval workflow for the list will give problem while enablind. We can notice it will add one extra parameter(Infopath_Sentinel=1) in the URL querystring. I am not sure what it is used for, but we cannot create the workflow.

So to avoid this we need te make sure the HTTPOnly cookie need not be enabled from the web.config.

Tuesday, April 10, 2012

Reading RSS feed

Recently while reading the data from the RSS feed, we faced an issue with the RSS not getting updated because of the Header cache. After analysis we found that its because of the caching on client server, we are not able to get the updated data.

To solve this we have tried many options, but the easiest option is to embed the querystring with GUID, which will make sure its always the fresh request.

Following is the sample code

string sSiteUrl = sFeedURL;
System.Net.WebClient webClient = new System.Net.WebClient();
webClient.Encoding = System.Text.Encoding.UTF8;
Guid guid = Guid.NewGuid();
sSiteContent = webClient.DownloadString(sSiteUrl+"?guid="+guid.ToString());
webClient.Dispose();