Saturday, January 21, 2012

Loop through all webs faster way in SharePoint 2010

We are learning so many concepts each day and implementing/writing lot of code. But, we might not spending time to write the code more efficient way or more cleaner way. So, this post is again related to how to write code efficient in SharePoint 2010. :)

So far, till SharePoint 2007 to loop through all the webs in site collection we use the code:
using(SPSite site = new SPSite("http://sitecollectionurl")
{
foreach(SPWeb web in site.AllWebs)
{
//Some code here
web.Dispose();
}
}
The above code is not at all wrong.

Case 1:
If you need the web object only for reading the generic information of the site like title, url, id etc… then it is a very expensive operation. For this, in SharePoint 2010 there is a workaround and that will load results 10 times faster than above code. Below is the efficient code:
using(SPSite site = new SPSite("http://sitecollectionurl")
{
foreach(SPWebInfo webInfo in site.AllWebs.WebsInfo)
{
//Code here to read web information 
}
}
This way you only reading the web information object instead of complete Web object. You can take a look more about WebInfo class here in MSDN.

Case 2:
If you want to read the properties you needed then there is a more better way than simply loop through AllWebs property in for each. The complete explaination is here. This is a very good post and very very faster way to read the properties in all webs.

You really see the difference, a big difference. Do you like this post?

Wednesday, January 11, 2012

Download Visual Studio 11 Developer Preview - now with SharePoint developer tools

Visual Studio 11 Developer Preview is now available with the SharePoint developer tools and downloadable to public. Earlier version only has the Windows 8 stuff as a new thing along with some user experience improvements. But, in the new version it has the SharePoint developer tools added. So, as I am a SharePoint guy I would like to see what are all the features available in the new version.

Visual Studio 11 is very fast when compared to Visual Studio 2010 and it definitely improves the productivity. This is one of the major point which I like about it. When come back to what are the new features added in SharePoint developer tools, below are the few of them.
  • Create Lists and Content Types by Using New Designers
  • Create Site Columns
  • Create Silverlight Web Parts
  • Publish SharePoint Solutions to Remote SharePoint Servers
  • Test SharePoint Performance by Using Profiling Tools
  • Create Sandboxed Visual Web Parts
  • Improved Support for Sandboxed Solutions.
  • Support for JavaScript Debugging and IntelliSense for JavaScript
  • Streamlined SharePoint Project Templates
  • Related Topics
The MSDN link has all these features explained. Stay tuned for the more updates from this blog as I like to publish more articles on what next...

Monday, December 12, 2011

Script to install SharePoint 2010 in Windows 7

You are ready to install SharePoint 2010 in Windows 7? Then you are at the right place. For the installation you have to download all required and follow the complete MSDN article and then execute step by step. If there is something which do all the steps for you then how it is? good right?
Yah! there is a great script which is written by Ram which installs SharePoint 2010 on windows 7 PC. Go and download and execute guys. Great script.

Get the information here.

How to find Inactive computer accounts in active directory?

Do you know how can we do this? Either we query the AD to get the information by writing code in C# or manually check the AD for the inactive accounts. But, there is another simple way which will get this information without any big efforts. Yes, using Powershell script.
$COMPAREDATE=GET-DATE
$NumberDays=90 
$CSVFileLocation='C:\TEMP\OldComps.CSV' 
GET-QADCOMPUTER -SizeLimit 0 -IncludedProperties LastLogonTimeStamp | where { ($CompareDate-$_.LastLogonTimeStamp).Days -gt $NumberDays } | Select-Object Name, LastLogonTimeStamp, OSName, ParentContainerDN | Sort-Object ModificationDate, Name | Export-CSV $CSVFileLocation 
You have to provide the days - the timeline of inactive accounts and where to save the output of inactive accounts list.

The complete information is available at this post.

Open PDF files directly in browser SharePoint 2010

Here is how you can tell SharePoint 2010 site to open the PDF files in the browser instead of prompting the user to save or open the PDF. This will be a great option to the most of end users. This needs simple Powershell scripting run on the server. Below is the code we should use:
# <# 
# .DESCRIPTION 
#  This script adds new MIME type to "AllowedInlineDownloadedMimeTypes" property list of defined SharePoint 2010 Web Application.
#
#  Script prompts you for MIME type and Web Application URL.
#
#  Code shall run in context of Farm Administrators group member.
# 
# .NOTES 
#       File Name   : Add_MIME_Type.ps1 
#       Author      : Kamil Jurik, WBI Systems a.s. 
#       Created     : 11/12/2011 
# 

If ( (Get-PSSnapin -Name "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue) -eq $null ) {
    Add-PSSnapin "Microsoft.SharePoint.PowerShell"
}
Get-SPWebApplication
$WebApp = Get-SPWebApplication $(Read-Host "Enter Web Application URL")
Write-Host "Mime Type Examples:""application/pdf, text/html, text/xml"

If ($WebApp.AllowedInlineDownloadedMimeTypes -notcontains ($MimeType = Read-Host "Enter a required mime type"))
{
  Write-Host -ForegroundColor White "Adding" $MimeType "MIME Type to defined Web Application"$WebApp.url
  $WebApp.AllowedInlineDownloadedMimeTypes.Add($MimeType)
  $WebApp.Update()
  Write-Host -ForegroundColor Green "The" $MimeType "MIME type has been successfully added."
} Else {
  Write-Host -ForegroundColor Red "The" $MimeType "MIME type has already been added."
}
The powershell script do the magic for us. Below is the explanation.
  1. It is taking the web application url as input
  2. And if the web application is not allowed the the PDF files MIME type to it's allow downloadable mime types collection then it will add it.
So, once you run the code the files will be open up in the browser instead of prompting the user. The server will not allow PDF files by default as it think PDF files are not secure.

Note: The script should run with the account who has FARM administrator level access.
For more details about it: please check technet article.

Sunday, December 4, 2011

ASP.NET 4.5 new features

The new version of ASP.NET 4.5 has many features integrated and there are many additions to the Visual Studio as well. I am really excited to see all of these features in hand.... These functionality improvements makes applications more easier and faster. The features are added in both Web forms and MVC.

ScottGu's blog has all these features explained in detail as series of posts. Readers, please read all of these and know the new and great features coming for better productivity.

ASP.NET 4.5 New Features

Thursday, October 13, 2011

Get TaskID in SharePoint custom Visual Studio workflows

When you are working with custom workflows implementing in Visual studio, you might be sending the emails when a task is created. By default SharePoint sends an email when a task is assigned to a user. But, if you want to customize the email body and subject, then you will use send mail activity and will give link to the user. Here the problem starts.
In CreateTask activity in Visual Studio you cannot get the taskID as the task is not yet created. What to do??? So, to get the task identifier we have to implement the below approach.
  1. CreateTask Activity
  2. TaskCreated Activity 
  3. SendMail Activity
  4. OnTaskChanged Activity
  5. TaskComplete Activity
Remember, until unless the Activity TaskCreated is executed the TaskID will not be available in workflow context. The main reason behind is, the task created information is not caught by SharePoint workflow anywhere until we run this activity. So, in the TaskCreated activity you can get the AfterProperties of the task and in those properties you will get the recent created task identifier which is integer.

So, note that in custom workflows which are implementing through Visual Studio the only way you can get the TaskID is AfterProperties of the TaskCreated activity. I have wasted hours when I was learning these. Hope you liked it.

The context has expired and can no longer be used–SharePoint and Reporting Services

Error:

"System.Web.Services.Protocols.SoapException: Report Server has encountered a SharePoint error. ---> Microsoft.ReportingServices.Diagnostics.Utilities.SharePointException: Report Server has encountered a SharePoint error. ---> Microsoft.SharePoint.SPException: The context has expired and can no longer be used. (Exception from HRESULT: 0x80090317) ---> System.Runtime.InteropServices.COMException: The context has expired and can no longer be used. (Exception from HRESULT: 0x80090317) --- End of inner exception stack trace --- at Microsoft.ReportingServices.WebServer.ReportingService2005Impl.GetDataSourceContents(String DataSource, DataSourceDefinition& Definition) at Microsoft.ReportingServices.WebServer.ReportingService2006.GetDataSourceContents(String DataSource, DataSourceDefinition& Definition)"

Solution:

This was coming completely due to the sever clock has wrong time set. So, by adjusting the time on the server solved the problem.

Special thanks to "Steve Mann" for finding the solution. It is a great find as it is just by guessing and thinking in different ways. Good one Steve.

Saturday, October 8, 2011

Close SharePoint 2010 Dialog through code

In SharePoint 2010, the new feature is added is the dialog framework. With the help of this, user stays on the same page and able to get the information without go away from the current page. So, there are ways that we can close the dialog from the code in different ways. Below are the mainly used implementations in javascript and code behind.
Javascript:
<script type='text/javascript'>window.frameElement.commitPopup()</script>

The window.frameElement is the node which holds the current dialog window. commitPopup() method which commits the popup and closes the dialog from the page window. So, by simply calling the above method, the dialog window will close automatically.

Code-Behind:
HttpContext context = HttpContext.Current;
if (HttpContext.Current.Request.QueryString["IsDlg"] != null){
context.Response.Write("<script type='text/javascript'>window.frameElement.commitPopup();</script>");
context.Response.Flush();
context.Response.End();
}

The dialog framework recognizes an additional parameter in the request named "IsDlg=1", which says open the page in dialog box. So, in code-behind simply check for query string parameter and if it exists then call the javascript method mentioned above by writing it to browser. So, when it executed the dialog will be closed.

This is a very nice tip which helps in some custom implementations. Hope it helps.