Tuesday, September 6, 2011

How to read web.config from a timer job in Sharepoint

Reading web.config from a timer job::

I gotta say that I am pretty proud about this one. The scenario is that you have a timer job that connects to a database somewhere, and the connection string is stored in the web.config file. Now since your timer job doesnt run inside of IIS, it runs under the OWSTIMER executable, you can’t access it directly. The workaround is very, very easy though – 2 lines of code in fact!
The first thing you need is a reference to the SPWebApplication object. Once you have your SPWebApplication you can get your configuration file through the WebConfigurationmanager class. Here is an example of an execute method of a timer job that will get a setting for you:
   1: public override void Execute(Guid targetInstanceId)
   2: {
   3:     SPWebApplication webApplication = this.Parent as SPWebApplication;
   4:  
   5:     Configuration config = WebConfigurationManager.OpenWebConfiguration("/", webApplication.Name);
   6:     String myValue = config.AppSettings.Settings["mySettingName"].Value;
   7: }
So a couple of notes about the above code:
  • You might be wondering what I use the settings property of the AppSettings property – the answer is that when I use AppSettings["mySettingName"] directly I was getting an error that it was not accessible due to its access level, but the Settings property works just as good
  • This code assumes that you have not gone and renamed your SharePoint sites in IIS directly – the OpenWebConfiguration method uses a relative site URL within the IIS site, and the name of the site as it appears in IIS to load the configuration file. By default with SharePoint this will always match up the the SPWebApplication’s name property, but it is something to keep in mind
The above code should work for any SharePoint web application, so this means you can read the web.config files from each web application that use your timer job (perfect for what I needed as the connection string was unique per web app). The coolest part about this though is that it is only two lines of code! When I had a quick Google search around for this I didn’t find much, most people just recommended storing the connection strings in the registry or something like that, so this is a nice alternative

How to get iframe src page title and then set main page title

To get iframe src page title and then set main page title::

Unless the webpage is in the iframe is from the same domain as the containing page, it is impossible.
If they do have the same domain, then try the following:
document.title = document.getElementById("iframe").documentElement.title; 



In case you anyway want to do it using jquery, you can do it as following:
var title = $( "#frame_id").contents( ).find( "title").html( ); 
$( document).find( "title").html( title); 


And pay attention as stated above, you can do it only when script running and page in the same domain, otherwise it's useless.

How to open PDF files,HTML files in browser in Sharepoint 2010

Below are the few methods to Open PDF files,HTM files in browser in Sharepoint 2010 ::

Web Application level setting: Method 1 (UI)

• Go to SharePoint 2010 Central Administration > Application Management > Manage Web Applications
• Select the row of your web application
• Click General Settings in the ribbon
• Scroll down to Browser File Handling and select Permissive
• Click Ok

Web Application level setting: Method 2

1
2
3
$webApp = Get-SPWebApplication http://intranet.domain/
$webApp.BrowserFileHandling = "permissive"
$webApp.update()


Web Application level setting: Method 3 (recommended)


1
2
3
4
5
6
7
8
9
10
$webApp = Get-SPWebApplication http://intranet.domain/
If ($webApp.AllowedInlineDownloadedMimeTypes -notcontains "application/pdf")
{
  Write-Host -ForegroundColor White "Adding Pdf MIME Type..."
  $webApp.AllowedInlineDownloadedMimeTypes.Add("application/pdf")
  $webApp.Update()
  Write-Host -ForegroundColor White "Added and saved."
} Else {
  Write-Host -ForegroundColor White "Pdf MIME type is already added."
}


Site Collection level


1
2
3
4
5
6
7
8
9
10
11
12
foreach ( $subsite in $site.allwebs ) 
 foreach ($list in $subsite.Lists) 
 { 
  if($list.browserfilehandling -eq "Strict") 
  { 
   $list.browserfilehandling = "Permissive"; 
   $list.update(); 
  } 
 } 
}


Site level ( SPWeb )


1
2
3
4
5
6
7
8
9
foreach ($list in $web.Lists) 
 if($list.browserfilehandling -eq "Strict") 
 { 
  $list.browserfilehandling = "Permissive"; 
  $list.update(); 
 } 
}


List Level


1
2
3
4
5
6
7
$list = $web.Lists["MyList"] 
if($list.browserfilehandling -eq "Strict") 
 $list.browserfilehandling = "Permissive"; 
 $list.update(); 
}



Hope this helps...

Popular Posts