Tuesday, June 21, 2011

SharePoint Interview Queations.

TCS :--

1) How do you display Dropdown list in WerPart Property pane?

Example : Dropdown Filter Web Part in SharePoint

2) How you can use Workflow tokens in VS Workflow?

3) Question on Form topology?

4) Questions on SDLC?

Displaying custom properties in the property Pane

Displaying custom properties in the property Pane

After you've imported your sample Web Part into a Web Part Page, you can display the property pane to view or change their values. A custom property will be displayed automatically in the default property pane if the property is of type string, bool, int or enum. The following table describes how each of these property types is displayed in the property pane.



Property Type-------- Displayed in property pane as

bool--------> Check box
DateTime --------> Text box
enum --------> Dropdown
int --------> Text box
string --------> Text box



To display or change the values of custom properties  



  • On the Web Part menu, click Modify This Web Part.

  • Expand the Custom Properties section of the property pane.



http://www.sharemuch.com/2009/07/21/dropdownlist-in-sharepoint-webpart-properties/


http://bilbrobloggins.com/sharepoint/dropdown-list-box-filter-web-part-in-sharepoint-ndash-part-three/

Friday, December 24, 2010

Editcontrolblock-customaction-for-sharepoint


Editcontrolblock-customaction-for-sharepoint

Thursday, December 23, 2010

Using Javascript code to show/hide controls in NewForm or EditForm.

You can use the following javascript code to show/ hide controls in NewForm or EditForm.

< script type="text/javascript" >

_spBodyOnLoadFunctionNames.push("hideFieldsOnStart");

function hideFieldsOnStart() {

//hide the control at startvar control = getTagFromIdentifierAndTitle("input","TextField","FieldDisplayName");

control.parentNode.parentNode.parentNode.style.display="none";

//add an onchange event to the dropdown
getTagFromIdentifierAndTitle("select","DropDownChoice","DropDownDisplayName").onchange = function() {ChangeEvent()};


}


function ChangeEvent()
{

//get the dropdownvar dropdown = getTagFromIdentifierAndTitle("select","DropDownChoice","DropDownDisplayName");

//get the selected valuevar option = dropDown.options[dropDown.selectedIndex].text;

//get the controlvar control = getTagFromIdentifierAndTitle("input","TextField","FieldDisplayName");

//show hide based on your conditionif(option == "xyz")
{
control.parentNode.parentNode.parentNode.style.display="";
}
else
{
control.parentNode.parentNode.parentNode.style.display="none";
}

//this gets the field based on title identifier and tagnamefunction getTagFromIdentifierAndTitle(tagName, identifier, title) {
var len = identifier.length;
var tags = document.getElementsByTagName(tagName);
for (var i=0; i < tags.length; i++) {
var tempString = tags[i].id;
if (tags[i].title == title && (identifier == "" || tempString.indexOf(identifier) == tempString.length - len)) {
return tags[i];
}
}
returnnull;
}
</script >

List of sharepoint column types and their corresponding identifiers, tagnames and Title


































































SharePoint Field Type Identifier Tag Name Title
Single Line of Text TextField input Same as Field Title
Multiple Lines of Text TextField input Same as Field Title
Number TextField input Same as Field Title
Currency TextField input Same as Field Title
Choice (dropdown) DropDownChoice select Same as Field Title
Lookup (single)* Lookup select Same as Field Title
Lookup (multiple) SelectCandidate; SelectResult select Same as Field Title
Yes/No BooleanField input Same as Field Title
People or Group UserField_downlevelTextBox textarea People Picker

(It must be

People Picker only. Irrespective of Filed name)


It depends what type of field you are trying hide it is in your case
if text field it should be
var control = getTagFromIdentifierAndTitle("input","TextField","FieldDisplayName");

if choice

var control = getTagFromIdentifierAndTitle("select","DropDownChoice","FieldDisplayName");

if yes/no then

var control = getTagFromIdentifierAndTitle("input","BooleanField","FieldDisplayName");

if Lookup then

var control = getTagFromIdentifierAndTitle("select","Lookup","FieldDisplayName");

If it is People Picker


var control = getTagFromIdentifierAndTitle('textarea','UserField_downlevelTextBox','People Picker');

Adding Context Menu in SharePoint List or Library using Content Editor Web part

< script type="text/javascript">
function Custom_AddListMenuItems(m, ctx) {
CAMOpt(m,'Edit Form(new window)','javascript:window.open(\'EditForm.aspx?ID=' + currentItemID + '\');','/_layouts/images/LIST.GIF');
// false means that the standard menu items should also be rendered
// if you set true, it will show only the menu which you added( standard sharepoint menu items will not be available.
// you can control by setting the value of true or false
return true;
}
</script>

If the above function returns false, then the context menu is displayed as below.




If the above function returns true, then the context menu is displayed as below.



Similary if you want to do for List, you have to use the Different method name.

For Document Library : Instead "Custom_AddDocLibMenuItems"

For List : "Custom_AddListMenuItems"

Also you can do following using Script in Content Editor Webpart

To hide the "Delete" (particular) menu item in LibraryContext Menu add this below function

function CAMOpt(p,wzText,wzAct,wzISrc,wzIAlt,wzISeq,wzDesc)
{
var mo=CMOpt(wzText,wzAct,wzISrc,wzIAlt,wzISeq,wzDesc);
if(!mo)return null;
//wzText you can give which one you want hide (like "Edit Properties")
if(wzText != "Delete") AChld(p,mo);
return mo;
}

You can download the script and add it in your content editor webpart; it will take care of the previous actions for document Library.

Use the approriate Script for list / library.
Enjoy!!!...

Using javascript to hide People Picker SharePoint field based on DropDown value

< script type="text/javascript">

_spBodyOnLoadFunctionNames.push("hidePeoplePicker");

function hidePeoplePicker() {
//alert("HIDE");
var control = getTagFromIdentifierAndTitle('textarea','UserField_downlevelTextBox','People Picker');
control.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.style.display="none";

//This code runs on the onchange event of the drop down list field
getField('select','Form Type').onchange = function() {getSecurityLevelValue()};

}

function showPeoplePicker() {
var control = getTagFromIdentifierAndTitle('textarea','UserField_downlevelTextBox','People Picker');
control.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.parentNode.style.display="";

}




function getTagFromIdentifierAndTitle(tagName, identifier, title) {

//alert("tagName = " + tagName);

var len = identifier.length;
//alert("Len = " + len);

var tags = document.getElementsByTagName(tagName);
//alert("tags length = " + tags.length);

for (var i=0; i < tags.length; i++) {
var tempString = tags[i].id;
var tt = tempString.indexOf(identifier);
var ss = tempString.length - len;
if(tags[i].title == "People Picker")
//alert("tempString = " + tt +"---"+ss );
if (tags[i].title == title && (identifier == "" ||
tempString.indexOf(identifier) == tempString.length - len)) {
return tags[i];
}
}
return null;
}




function getField(fieldType,fieldTitle) {

var docTags = document.getElementsByTagName(fieldType);

//alert("Length = " + docTags.length);

for (var i=0; i < docTags.length; i++) {

if (docTags[i].title == fieldTitle) {

//alert("Value in getField = " + docTags[i].value);

return docTags[i];

}

}

}

function getSecurityLevelValue() {

//alert("In getSecurityLevelValue");
selectedId = getField('select','Form Type').options[getField('select','Form Type').selectedIndex].value;

//alert("Value in hide =" + selectedId);

if(selectedId != 'Org change/ cost centre change')

{

//alert("NOT Specific Request Hide");

hidePeoplePicker();

}

else

{

//alert("Request Show");
showPeoplePicker();

}

}

</script>

Note:-

1) If you have more than one People Picker either in edit form or new form, in that case hidden field should be displayed first and then non hidden people picker in that form. It is done by changing Column order.

Friday, August 6, 2010

Understanding Database Stored Procedures

This article discusses stored procedures and how to create, update, and delete stored procedures using SQL.

What is a Stored Procedure?

A stored procedure is a database object that contains one or more SQL statements. In this article you will get an idea on how to create and use stored procedures and also highlighted on how to use stored procedures.

The first time a stored procedure is executed; each SQL statement it contains is compiled and executed to create an execution plan. Then procedure is stored in compiled form with in the database. For each subsequent execution, the SQL statements are executed without compilation, because they are precompiled. This makes the execution of stored procedure faster than the execution of an equivalent SQL script.

To execute a stored procedure you can use EXEC statement.

CREATE PROC spGetShashiAS SELECT * FROM SHASHI

When you run this script in Pubs database you will get the following message in Query Analyzer.

The Command(s) completed successfully.

Now you are ready to call/execute this procedure from Query Analyzer.

EXEC spGetShashi

This stored procedure creates a result set and returns to client.

You can call a stored procedure from within another stored procedure. You can even call a stored procedure from within itself. This technique is called a recursive call in programming. One of the advantages of using stored procedures is that application programmers and end users don't need to know the structure of the database or how to code SQL. Another advantage of it is they can restrict and control access to a database.

Classification of Stored Procedures

The classification of stored procedures is depends on the Where it is Stored. Based on this you can divide it in 4 sections.

  • System stored procedures
  • Local stored procedures
  • Temporary stored procedures
  • Extended stored procedures

System stored procedures are stored in the Master database and are typically named with a sp_ prefix. They can be used to perform variety of tasks to support SQL Server functions that support external application calls for data in the system tables, general system procedures for database administration, and security management functions.
For example, you can view the contents of the stored procedure by calling

sp_helptext [StoredProcedure_Name].

There are hundreds of system stored procedures included with SQL Server. For a complete list of system stored procedures, refer to "System Stored Procedures" in SQL Server Books Online.

Local stored procedures are usually stored in a user database and are typically designed to complete tasks in the database in which they reside. While coding these procedures don't use sp_ prefix to you stored procedure it will create a performance bottleneck. The reason is when you can any procedure that is prefixed with sp_ it will first look at in the mater database then comes to the user local database.

A temporary stored procedure is all most equivalent to a local stored procedure, but it exists only as long as SQL Server is running or until the connection that created it is not closed. The stored procedure is deleted at connection termination or at server shutdown. This is because temporary stored procedures are stored in the TempDB database. TempDB is re-created when the server is restarted.

There are three types of temporary stored procedures: local , global, and stored procedures created directly in TempDB.

A local temporary stored procedure always begins with #, and a global temporary stored procedure always begins with ##. The execution scope of a local temporary procedure is limited to the connection that created it. All users who have connections to the database, however, can see the stored procedure in Query Analyzer. There is no chance of name collision between other connections that are creating temporary stored procedures. To ensure uniqueness, SQL Server appends the name of a local temporary stored procedure with a series of underscore characters and a connection number unique to the connection. Privileges cannot be granted to other users for the local temporary stored procedure. When the connection that created the temporary stored procedure is closed, the procedure is deleted from TempDB.

Any connection to the database can execute a global temporary stored procedure. This type of procedure must have a unique name, because all connections can execute the procedure and, like all temporary stored procedures, it is created in TempDB. Permission to execute a global temporary stored procedure is automatically granted to the public role and cannot be changed. A global temporary stored procedure is almost as volatile as a local temporary stored procedure. This procedure type is removed when the connection used to create the procedure is closed and any connections currently executing the procedure have completed.

Temporary stored procedures created directly in TempDB are different than local and global temporary stored procedures in the following ways:

  • You can configure permissions for them.
  • They exist even after the connection used to create them is terminated.
  • They aren't removed until SQL Server is shut down.

Because this procedure type is created directly in TempDB, it is important to fully qualify the database objects referenced by Transact-SQL commands in the code. For example, you must reference the Authors table, which is owned by dbo in the Pubs database, as pubs.dbo.authors.

--create a local temporary stored procedure.

CREATE PROCEDURE #tempShashiAS
SELECT * from [pubs].[dbo].[shashi]

--create a global temporary stored procedure.

CREATE PROCEDURE ##tempShashi AS
SELECT * from [pubs].[dbo].[shasi]

-create a temporary stored procedure that is local to tempdb.

CREATE PROCEDURE directtempAS
SELECT * from [pubs].[dbo].[shashi]

An extended stored procedure uses an external program, compiled as a 32-bit dynamic link library (DLL), to expand the capabilities of a stored procedure. A number of system stored procedures are also classified as extended stored procedures. For example, the xp_sendmail program, which sends a message and a query result set attachment to the specified e-mail recipients, is both a system stored procedure and an extended stored procedure. Most extended stored procedures use the xp_ prefix as a naming convention. However, there are some extended stored procedures that use the sp_ prefix, and there are some system stored procedures that are not extended and use the xp_ prefix. Therefore, you cannot depend on naming conventions to identify system stored procedures and extended stored procedures.

Use the OBJECTPROPERTY function to determine whether a stored procedure is extended or not. OBJECTPROPERTY returns a value of 1 for IsExtendedProc, indicating an extended stored procedure, or returns a value of 0, indicating a stored procedure that is not extended.

USE MasterSELECT OBJECTPROPERTY(object_id('xp_sendmail'), 'IsExtendedProc')

Create a Stored Procedure 

CREATE PROCEDURE SQL statement is used to create an SP. When the CREATE PROCEDURE statement is executed, the syntax of the SQL statements within the procedure is checked. If you have made a coding error the system responds with an appropriate message and the procedure is not created.

The Syntax of the CREATE PROCEDURE statement


CREATE {PROC|PROCEDURE} Procedure_name
[Parameter_declaration]
[WITH {RECOMPILE|ENCRYPTION|RECOMPILE, ENCRYPTION}]
AS sql_statements


You can use CREATE PROCEDURE statement to create a stored procedure in the database. The name of the stored procedure can be up to 128 characters and is typically prefixed with the letters sp.

If you look at the above options like AS, RECOMPILE, ENCRYPTION these are having some significance meaning to it.

The AS clause contains the SQL statements to be executed by the stored procedure. Since a stored procedure must consist of single batch.

Recompile is used when you want to compile the stored procedure every time when you call. This comes into the picture when one doesn’t want to catch the execution plan of stored procedure in the database. Encryption implies that you want to hide this code so that no one can see it. This is very important when you want to distribute the code across the globe or when you want to sell this code to other vendors. But make sure you have original copy it; because once you encrypted it no one can decrypt it.

Apart from the stored procedure that store in the database a permanent entity you can create stored procedure as per you session. That means as long the as the session is alive then the stored procedure is available in the memory means in the database.
Once the session ends the stored procedure is vanished this actually depends on what type of stored procedure you have chosen to create it.

Stored procedure provide for two different types of parameters: input parameters and Output Parameters. An input Parameter is passed to the stored procedure from the calling program. An output parameter is returned to the calling program from the stored procedure. You can identify an output parameter with the OUTPUT keyword. If this keyword is omitted the parameter is assumed to be an input parameter.

You can declare an input parameter so that it requires a value or so that its value is optional. The value of a required parameter must be passed to the stored procedure from the calling program on an error occurs. The value of an optional parameter doesn’t need to be passed from the calling program. You identify an optional parameter by assigning a default value to it. Then if a value isn’t passed from the calling program, the default value is used. You can also use output parameter as input parameters. That is you can pass a value from the calling program to the stored procedure through an output parameter. However is not advisable to pass parameters to Output parameters.

The syntax for declaring the parameters

@Parameter_name_1 data_type [= default] [OUTPUT]
[, @Parameter_name_2 data_type [= default] [OUTPUT]


Parameter declarations:
@FirstName varchar(50) -- Input parameter that accepts a string.
@LastName varchar(50) -- Output Parameter that returns a string.

Create Procedure statement that uses an input and an output parameter.

CREATE PROC spGetShashi
                       @FirstName varchar(50),
                       @LastName varchar(50)
AS
SELECT  @LastName= ln_Name
FROM    SHASHI
WHERE   fn_name = @FirstName


Create procedure statement that uses an optional parameter.

CREATE PROC spGetShashi
                       @LastName varchar(50),
                       @FirstName varchar(50) = 'shashi'
AS
SELECT  @LastName= ln_Name
FROM    SHASHI
WHERE   fn_name = @FirstName


A stored procedure can declare up to 2100 parameters. If you declare two or more parameters, the declarations must be separated by commas.

Delete or Update a Stored Procedure

You use DROP PROC SQL statement to delete one or more stored procedures from database. To update the stored procedure you use ALTER PROC SQL statement.

The syntax of the DROP PROC statement

DROP {PROC|PROCEDURE} Procedure_name [, ]

The syntax of the ALTER PROC statement

ALTER {PROC|PROCEDURE} Procedure_name[Parameter_declaration][WITH {RECOMPILE|ENCRYPTION|RECOMPILE, ENCRYPTION}]AS sql_statements

When you delete a procedure any security permission that are assigned to the procedure are also deleted. In that case you will want to use the ALTER PROC statement to modify the procedure and preserve permissions.

Calling stored procedure with Parameters

To pass parameter values to a stored procedure, you code the values in the EXEC statement after the procedure name. You can pass the parameters either by position or by name.

Passing parameters by Name:

Write the following code in Query Analyzer

DECLARE @LN VARCHAR(100)EXEC spGetShashi @FirstName = 'shashi', @LastName = @LN OUTPUT

Passing parameters by Position:

DECLARE @LN VARCHAR(100)EXEC spGetShashi @LN OUTPUT, 'shahsi'

In fact you can use both notations to pass parameters to stored procedures when you are calling. To pass parameters by position, list them in the same order as they appear in the CREATE PROCEDURE statement and separate them with commas. When you use this technique, you can omit optional parameters only if they are declared after any required parameters.

To use an output parameter in the calling program, you must declare a variable to store its value. Then you use the name of the variable in the EXEC statement and you code the OUTPUT keyword after it to identify it as an output parameter.

Handling error in stored procedure

In addition to passing output parameters back to the calling program, stored procedures also pass back a return value. By default, this value is zero. If an error occurs during the execution of a stored procedure you may want to pass a value back to the calling environment that indicates the error that occurred. To do that you use the RETURN statement and the @@ERROR function.

The @@ERROR system function returns the error number that's generated by the execution of the most recent SQL statement. If the value is zero, it means that no error has occurred. The stored procedure listed below uses this function to test whether a DELETE statement that deletes a row from authors table is successful.

CREATE PROC spDeleteShashi @FirstName varchar(50) AS
DECLARE @ErrorVar int
DELETE FROM SHASHI WHERE fn_name = @FirstName
SET @ErrorVar = @ERRORIF @ErrorVar <> 0        
BEGIN              
   PRINT 'An Unknown Error Occurred'             
   RETURN @ErrorVar       
END

The RETURN statement immediately exists the procedure and returns an optional integer value to the calling environment. If you don't specify the value in this statement the return value is zero.

Stored Procedures part 2

In this article I will explain you about stored procedure.
How to create, execute and alter stored procedure.
Why to use stored procedures and advantages of stored procedures. 


  • A stored procedure is a group of Transact-SQL statements compiled into a single execution plan.
  • Stored Procedures are coding block in database server. It is pre compiled entity i.e. it is compiled at once and can be used again and again.
  • With help of stored procedure a group of SQL statements can be executed sequentially.
  • To supply data to the procedure we must have to use parameters in procedure.
  • Stored procedures use parameters mapping concept.
  • In parameter mapping front end and procedure parameters names, type and direction must be same and where front-end parameter length should be less than or equal to procedure parameter length (than only can map parameters).
  • To return any value from procedure we use return statement.

How to create a stored procedure

 
 

create
procedure insertData


(@RollNo int,

@Name varchar(50),

@Fees float)

 
 

as

begin

insert
into student values(@RollNo,@Name,@Fees)


select
*
from student


end

 
 

How to execute a stored procedure

 
 

exec insertData 8,
'Mahesh', 5600


 
 

Output of above stored procedure


 



 

Modifying a Stored Procedure

 
 

alter
procedure insertData


(@RollNo int,

@Name varchar(50),

@Fees float)

 
 

as

begin

insert
into student values(@RollNo,@Name,@Fees)


select
*
from student


end

 
 


 

Why to use stored procedures?

 
 

You normally write SQL statements, like select, inserts, updates to access your data from database. If you find yourself using the same query over and over again, it would make sense to put it into a stored procedure.

 
 

Every time you write a query it is parsed in database. If you have written a stored procedure for it, it will be parsed once and can be executed N number of times.

 
 

Stored procedures can also improve performance. All the conditional logic and is written into a stored procedure which is a single execution block on the database server.

 
 

Advantages of stored procedure

 
 

Modular programming

Stored Procedures are coding block in database server. IT is pre compiled entity i.e. it is compiled at once and can be used again and again.

Performance

Stored procedures provide faster code execution and reduce network traffic.

 
 

Faster execution: Stored procedures are parsed and optimized as soon as they are created and the stored procedure is stored in memory. This means that it will execute a lot faster than sending many lines of SQL code from your application to the SQL Server. Doing that requires SQL Server to compile and optimize your SQL code every time it runs.

 
 

Reduced network traffic: If you send many lines of SQL code over the network to your SQL Server, this will impact on network performance. This is especially true if you have hundreds of lines of SQL code and/or you have lots of activity on your application. Running the code on the SQL Server (as a stored procedure) eliminates the need to send this code over the network. The only network traffic will be the parameters supplied and the results of any query.

Security

Users can execute a stored procedure without needing to execute any of the statements directly.

 
 

Stored procedure can provide advanced database functionality for users who wouldn't normally have access to these tasks, but this functionality is made available in a tightly controlled way.

 
 

Conclusion

 
 

Hope this article might have helped you in understanding stored procedures.

 
 

Your feedback and constructive contributions are welcome. Please feel free to contact me for feedback or comments you may have about this article.