Thursday, December 23, 2010

Display Success and Error messages in SharePoint 2010

One of the common functions which is required from any developer; is to display a Success and Error messages to the user, and it’s always better to use the same style of the main application.
In SharePoint: SPUtility class contains two methods to achieve this.
Success Message:

SPUtility.TransferToSuccessPage: It is used to display a Success Message, and it has two overloads.
SPUtility.TransferToSuccessPage Method (String message)

Example:
SPUtility.TransferToSuccessPage("Welcome, You are in Success Page");

Figure (1) shows the output of this code. In this case the “Ok” button will redirect to the current SharePoint Site home page.


Figure(1) Success Page

SPUtility.TransferToSuccessPage Method (String message, String nextUrl, String linkText, String linkUrl)

Example:
SPUtility.TransferToSuccessPage("Welcome, You are in Success Page", "/SitePages/Continue.aspx", "", "");
In this case the “Ok” button will redirect to “"/SitePages/Continue.aspx” page.

Error Message:
The same is applied to SPUtility.TransferToErrorPage: it is used to display error message.

SPUtility.TransferToErrorPage(String message)
Example:
SPUtility.TransferToErrorPage("Sorry it seems that you did a mistake. This is the error page"); 


Figure (3) Error Page

Wednesday, December 15, 2010

SQL 2008 DateTime dataTypes

SQL 2008 introduced new types of DateTime data type (Time, Date, DateTime, DateTime2, SmallDateTime and DateTimeOffset).
The following query example explains the difference between each one of them.
SELECT 
CAST('2007-05-08 12:35:29. 1234567 +12:15' AS time(7)) AS 'Time' 
,CAST('2007-05-08 12:35:29. 1234567 +12:15' AS date) AS 'Date' 
,CAST('2007-05-08 12:35:29.123' AS smalldatetime) AS 'Smalldatetime' 
,CAST('2007-05-08 12:35:29.123' AS datetime) AS 'Datetime' 
,CAST('2007-05-08 12:35:29.1234567+12:15' AS datetime2(7)) AS  'Datetime2'
,CAST('2007-05-08 12:35:29.1234567 +12:15' AS datetimeoffset(7)) AS   'Datetimeoffset'
,CAST('2007-05-08 12:35:29.1234567+12:15' AS datetimeoffset(7)) AS 'Datetimeoffset IS08601';

Here is the result set.


Data type

Output

Time

12:35:29. 1234567

Date

2007-05-08

Smalldatetime

2007-05-08 12:35:00

Datetime

2007-05-08 12:35:29.123

datetime2

2007-05-08 12:35:29. 1234567

Datetimeoffset

2007-05-08 12:35:29.1234567 +12:15



Reference:
MSDN

Sunday, December 12, 2010

Filter list of SQL objects in SQL Management Studio

As I believe that to increase your productivity; you have to master using the tools which you are using. SQL Management Studio is one of the tools which is used by any developer during the day.
If you are working in a large project and you have a large number of objects (Tables, Stored Procedures, Functions or Views) in the application database, and you want to edit a certain one. Instead of scrolling up and down and reading the names, you can easily filter the list using the Filter context menu.
The following screen-shots present how you can find a table by name and Creation date.






Friday, December 10, 2010

How to repair a SQL Server 2005/ 2008 Suspect database

Some times after Hardware or Power failure, when you open the SQL Management studio; you find that some of your databases are not accessible, and they are marked as “Suspect”
To fix this you can run the following query (replace “MyDataBaseName” with the database name):
ALTER DATABASE MyDataBaseName SET EMERGENCY
DBCC checkdb ('MyDataBaseName')
ALTER DATABASE MyDataBaseName SET SINGLE_USER WITH ROLLBACK IMMEDIATE
DBCC CheckDB ('MyDataBaseName', REPAIR_ALLOW_DATA_LOSS)
ALTER DATABASE MyDataBaseName SET MULTI_USER

Source:
http://gargmanoj.wordpress.com/2008/07/17/how-to-repair-a-sql-server-2005-suspect-database/

Get list of Months Names in the current Thread language

One of the functions which are required in the localized applications is to get the list of months names in the current culture language.
To do this, some people do this using resources and add it manually to the control (DropDownList, ListBox, etc...), but you can accomplish this by the following single line of code which returns the months names translated to the current Thread culture.
Thread.CurrentThread.CurrentCulture.DateTimeFormat.MonthNames.Take(12);

Demo:
If the current thread culture is “fr-FR” it returns:

If the current thread culture is “de-DE” it returns:


If the current thread culture is “ar-EG” it returns:

I hope that this saves some of your valuable time.

Wednesday, December 8, 2010

Auto Sign out SharePoint

One of the customer requirements was "If the user is not the using the SharePoint portal in the browser for more than 10 minutes; The session should be ended for security reasons".
To accomplish this  I tried to enable the Session State for the SharePoint site "Which is disabled by default" and changed the Session time out, but it didn't work. So after a lot of research and googling, I decided to do it by JavaScript and with the help of the magic of JQuery:
I added the following java script block in the master page and it works fine , and the customer is satisfied.

<script src="http://code.jquery.com/jquery-1.4.4.js">
 
<script> $(document).ready(function(){  window.setTimeout(  
function(){   ie 
= (document.all) ? 
true : false;
if(ie){ document.execCommand('ClearAuthenticationCache',false);   }
var pathname = window.location.pathname;
window.location='/_layouts/AccessDenied.aspx?loginasanotheruser=true&amp;Source='  
pathname ;  }, 600000); }); 
</script>


I hope it helps you.

Sunday, December 5, 2010

Insert multiple rows values in one Insert statement (SQL 2008)

One of the nice tricks of insert statement; that it is possible to insert multiple rows values in one insert statement; you don’t need to insert each row in a separate statement.

CREATE TABLE DemoTable
    (
      ItemID INT IDENTITY
                 PRIMARY KEY,
      FirstName VARCHAR(50),
      LastName VARCHAR(50),
      CountryCode VARCHAR(4),
    )

INSERT  [DemoTable]
        (
          [FirstName],
          [LastName],
          [CountryCode]
        )
VALUES  ('Mahmoud','Hakeem','EG'),
        ('Michel','Durian','DE'),       
        ('Thomaz','Miracle','Fr')
 

Tuesday, November 9, 2010

Using code to create InfoPath Custom Rule Condition

The following article explains how to create a custom rule condition using .NET code, and use it by the infopath designer.

http://www.infopathdev.com/blogs/greg/archive/2004/09/15/Use-Code-to-Determine-a-Rule-Condition.aspx

I hope it helps you.

Friday, November 5, 2010

5 Troubleshooting Tips and Tricks for New SharePoint Developers

Q: I have a functioning web page, then after I deploy my solution package, I get a 404 error message. If I retract my solution, the page is still there. What happened?

A: A 404 error indicates the page is truly gone. However, you've pointed out that the page isn't really gone; it's still there. That would leave you to believe that it's some other resource that's missing. Well, what other resource is used by a page in SharePoint? That's right, a master page! If you get a 404 error on every page in your site, it probably means the master page property for your site has been set to a URL of a master page that doesn't exist. To fix this problem, type in the URL for the Site Settings page (which should always work, even if your master page is broken.) The Site Settings page's URL is the URL of your site, followed by /_layouts/settings.aspx. From the Site Settings page, click on the "Master page" link under the Look and Feel section to reset your master page to an out of the box one. This will at least get your site usable in the meantime.

If you notice you get a 404 error on only one page in your site, and that page is a publishing page, it could be that its associated page layout was not deployed properly


Q: We're using source control to manage our Visual Studio 2010 project. Someone added a file to the project and now when I try to deploy my solution, on the Feature Activation step, it says it can't activate a feature. What happened?
Visual Studio keeps track of which files are in which node (i.e. which files are part of a Module node, an Element node, a mapped folder, etc.). If you click on the "Show all files" button at the top of your Solution Explorer in Visual Studio, you'll see that each node or folder has a file called SharePointProejctItem.spdata. Every time you move a file from one location to the other, or you delete or add a file, Visual Studio automatically makes changes to the .spdata file to reflect the change. If you are using Source Control, it could be that that file is checked in, it's locked, so Visual Studio can't make the appropriate changes to it, at which point the file is inconsistent with the files in its folder or node. In the case of the example mentioned in the question, you can run into problems if an old feature was removed and Visual Studio still thinks it's there and is trying to activate it.


Q: I modified the web.config file in my IIS web application folder, but I still can't seem to get errors to show up, even though I set the customErrors node to "Off". Why can't I see my error messges?
Believe or not, there is an additional web.config file in the LAYOUTS directory of your SharePoint root folder (at ~/14/TEMPLATE/LAYOUTS). You need to set the customErrors node to "Off" here, too.


Q: I built a page layout in SharePoint Designer and then copied the markup into Visual Studio so I could deploy it as a solution package. It deployed it the first time successfully, but now, when I make changes to it and redeploy it, it doesn't reflect my changes. Why not?
A: When you create a page layout in SharePoint Designer, the following tags automatically get added to the Page directive at the very top of the page:
meta:webpartpageexpansion="full" meta:progid="SharePoint.WebPartPage.Document
These tell SharePoint that the page is customized. When you deploy your page layout using a solution package, even if it has never been customized, and should be pointing to the markup file on the file system in your feature folder, SharePoint will see those tags and think the page layout has been customized, so it will no longer look at the file system. Make sure you always remove these tags from the top of your page before you try to deploy your page layout from a feature.


Q: I'm using LINQ to SharePoint but I'm noticing that not all my publishing columns are included in the class file that gets generated by SPMetal. I need to use those fields! What do I do?
A helpful solution I got from Andrew Connell is to create a partial class with your publishing column information. This will combine with the automatically generated class information to create a fully functioning class. Read about it here: http://www.andrewconnell.com/blog/archive/2010/07/28/extending-linq-to-sharepoint-entity-models-for-sharepoint-server-2010.aspx.


Source Becky Bertram blog

Introduction to Aspect Oriented Programming

Aspect Oriented Programming (AOP) is a new programming paradigm dealing with separating crosscutting concerns that are usually hard to do in object-oriented programming. An aspect is a software entity implementing a specific non-functional part of the application. Click Here to read this nice article which gives a valuable introduction about it.

Friday, October 29, 2010

Implementing Two interface having the same method signature in the same class

I was searching for how to implement two interfaces having the same method, and I found this nice article by Sandeep P R describing this.

Thursday, September 30, 2010

Log Exception.ToString(); never log only Exception.Message!

When you are trying to log an exception, don't forget that you should always log
Exception.ToString();

And never use
 Exception.Message

The first snippet of code gives you more information about the exception like Stack trace, Inner exception and Message.
If you only log Exception.Message, you'll only have something like "Object reference not set to an instance of an object".

Thursday, August 12, 2010

Infragistics WebDatePicker AutoPostBack

WebDatePicker is one of the nice controls provided by Infragistics company. This control has a nice user interface as well many other nice features
Figure(1) Infragistics WebDatePicker


To configure this control to fire postback when selecting a value from the calendar, there is a small trick you have to do. The default value of the property “Edit Mode” is “KeyboardAndCalendar” which causes the postback event to be fired after the control loose focus.

Figure(2) WebDatePicker Properties

To fire the postback event when the user selectes a value, you have to change the property value to “CalendarOnly”.

Saturday, February 27, 2010

Which is better for performance, Sunbroutine Or Function in VB.NET?

Many VB.NET developer are confused about the difference between the Function and Subroutine in VB.NET.
Here is a nice article from CodeProject, explains the differences between them and when to use each one of them Click Here

Friday, February 26, 2010

Have you ever used a Virtual Database?

During the last decades, The concept of Virtualization covered most of the fields/ elements  of the IT field, starting from virtual hardware devices, operation systems and software simulation.
But, Have you ever used a VIRTUAL DATABASE?

I found a nice article demonstrating a Virtual Database tool. The company called Idera produced a tool that attach a backup file to the SQL Server and it appears as a normal database. You can run queries, run any third Party tools directly against this database.You can do all these steps in less than 3 minutes.

But, Why I need to use a tool like this?
The big advantage of a tool like this is Save your time (Time is money).
If you are working in a project and you have a production database backup, with a size 15 GB, and you want to synchronize the latest changes from the development instance.
Here is the traditional steps to do this.
1- Create a new database.
2- Restore the production database backup on the new database.
3- Synchronize the changes between the two databases.
4- Save the synchronization SQL Script.

But with a third party tool like this:
1- Attach the backup file directly to the SQL server.
2- Synchronize the changes between the two databases.
3- Save the SQL Script.

You could say, I only saved one step out of 4.
I can tell you, from my experience, the longest step in all these 4 steps is step number 2.
So you removed the longest step, which usually takes about 10 minutes and my be more for the large size databases.
 There are many tools in the market doing this job. One of the greatest companies is Red Gate.
I am using the tools from Red Gate since 2 years, which solve all my hard SQL situations easily and in short time.

Any comments would be appreciated.

Friday, February 19, 2010

How to debug Dll deployed to the GAC remotely

Remote debugging is a very nice tool from Microsoft, especially if you have an error in a machine other than your development one, and you want to know what's the problem.
Firstly: Here are the steps to configure it from MSDN team Click Here.
These steps working fine with the mentioned example in the video.
But, How to use it if you have a SharePoint solution deployed to the GAC?
I spent 2 hours trying to let it work, but the point is,  remote debugging doesn't work if you don't have the "pdb" file in the same path with the "dll", so you have to copy the  "pdb" file to the same path with the "dll" one (If the soultion name is mysolution so, mysolution.pdb and mysolution.dll are the files in your debug folder from Visual Studio).
So, Here is one of the secrets of the GAC folder.
1- Open this Path %windir%\assembly this is the Assembly folder.


2- Now, Open the following Path %windir%\assembly\GAC_MSIL



3- You will find that for each dll deployed in the GAC, there is a folder with the same name.
4- Open mysolution folder
5- You will find that inside this folder, there is a folder for each version.
6- Open the version folder, and copy the mysolution.pdb and mysolution.dll to that location.
7- Start remote debugging and have fun :-)
I tried these steps and it's working fine with me.

You are welcome to post Comment Or Questions.

Sunday, February 14, 2010

SharePoint Interview Questions

A nice series of articles by Isha Attlee about SharePoint Interview Questions, for people who are working in SharePoint Development, Testing and Administration.
I found it useful for me, and it added to me many new tips.
For more information Click Here

Monday, February 8, 2010

Virtual Machines for SP Enviroment

If you have a laptop and you want to play around with the new features of SharePoint 2010, what are you going to do?
A nice article demonestrates the diffrenet possibilities to do this Click Here

Sunday, February 7, 2010

PDFMyURL Saves Web Sites as PDF Files [PDF]

PDFMyURL Saves Web Sites as PDF Files [PDF]
 
Saving something as a PDF file is a great way to preserve it for future reference or for sharing with others, without risking the site changing before you look at it again. PDFMyURL makes it easy to convert sites to PDF.
Point PDFMyURL at a website URL and it will convert the site into a PDF document. Not only can you do a simple conversion just by plugging in a URL but you can also modify the PDF with a wide variety of flags see the advanced menu for a full list that let you set the page orientation and size, header information, print orientation, and more. PDFMyURL also has a bookmarklet you can drag to your toolbar for easy access to the PDF creation service.

PDFMyURL is a free service and doesn't watermark or otherwise alter the site you are converting to PDF. Have a favorite tool for PDF conversion or a bookmarklet that makes life easier? Let's hear about it in the comments.

How to Reference a JavaScript file from a Content Page

A nice article descripes how to reference a JavaScript file from a content page instead of putting it in the master page, while you don't need it in all the pages.
For more Information Click Here