Tuesday, August 14, 2012

Show Tooltip to each element of dropdown

Add following code in master page of your application (make sure you have jquery in your application).

<script language="javascript" type="text/javascript">
        $(document).ready(function () {
            $("select").attr("title", function () {
                return $(this).find("option:selected").text();
            });

            $("select").change(function () {
                $(this).attr("title", function () {
                    return $(this).find("option:selected").text();
                });
            });

            $("select").each(function () {
                var i = 0;
                var s = this;
                for (i = 0; i < s.length; i++) {
                    s.options[i].title = s.options[i].text;
                    // the following  code is not working in IE so added saperate methods above
                   /* if (s.selectedIndex > -1) {
                        s.onmousemove = function () {
                            s.title = s.options[s.selectedIndex].text;
                        };
                    }*/
                }
            });


        });

    </script>

Monday, June 4, 2012

Get Documents from SharePoint Content Database in C# console application

If the SharePoint's Database is not attached with any  web application and you want to get the documents from that content db, for that purpose here is option to do the same

 

Create console application in Visual studio to get the document from Content DB, to access the documents use following Query.

 

 

SELECT AllLists.tp_Title AS [ListName],AllDocs.LeafName AS [FileName],AllDocs.DirName AS [URL],AllDocStreams.Content  AS [DocumentContent] FROM AllDocs JOIN AllDocStreams ON AllDocs.Id=AllDocStreams.Id JOIN AllLists ON AllLists.tp_id = AllDocs.ListId where AllLists.tp_Title='Documents';

 

 

In console application create following methods to get the documents.

 

 

Main method of console app to create data table object and download documents from SQL.

 

      public static void Main(string[] args)

        {

            Console.WriteLine("Application Started...");

            string strQuery = "SELECT AllLists.tp_Title AS [ListName],AllDocs.LeafName AS [FileName],AllDocs.DirName AS [URL],AllDocStreams.Content" +

                " AS [DocumentContent] FROM AllDocs JOIN AllDocStreams ON AllDocs.Id=AllDocStreams.Id JOIN AllLists ON AllLists.tp_id = AllDocs.ListId where AllLists.tp_Title='Documents'";

            SqlCommand cmd = new SqlCommand(strQuery);

            //cmd.Parameters.Add("@id", SqlDbType.Int).Value = 1;

            DataTable dt = GetData(cmd);

            if (dt != null)

            {

                download(dt);

            }

            Console.WriteLine("Press any key to complete.");

            Console.ReadKey();

        }

 

 

 

This download method will create folder hierarchy as in Document library and then it will create documents in particular folder.

 

        private static void download(DataTable dt)

        {

            int counter = 0;

            foreach (DataRow row in dt.Rows)

            {

                Byte[] bytes = (Byte[])row["DocumentContent"];

                string FileURL = Convert.ToString(row["URL"]);

                string ListName = Convert.ToString(row["ListName"]);

                string FileName = Convert.ToString(row["FileName"]);

 

                string documentFolderName = "Documents";

                if (!Directory.Exists(documentFolderName))

                {

                    Directory.CreateDirectory(documentFolderName);

                }

 

                string folderstructure = FileURL.Substring(FileURL.IndexOf(documentFolderName) + documentFolderName.Length+1);

 

                string[] folderstrArray = folderstructure.Split('/');

                string folderName = "Documents";

                for (int i = 0; i < folderstrArray.Length; i++)

                {

                    folderName =folderName +"\\"+ folderstrArray[i];

                    if (!Directory.Exists(folderName))

                    {

                        Directory.CreateDirectory(folderName);

                    }

                }

                using (Stream output = File.OpenWrite(folderName + "\\" + FileName))

                {

                   output.Write(bytes, 0, bytes.Length);

                }

 

               counter++;

            }

        }

 

 

 

Get the data table object from sql command.

 

        private static DataTable GetData(SqlCommand cmd)

        {

            DataTable dt = new DataTable();

            String strConnString = "----------------Add your connection string here.-----------------------------------";

            SqlConnection con = new SqlConnection(strConnString);

            SqlDataAdapter sda = new SqlDataAdapter();

            cmd.CommandType = CommandType.Text;

            cmd.Connection = con;

            try

            {

                con.Open();

                sda.SelectCommand = cmd;

                sda.Fill(dt);

                return dt;

            }

            catch

            {

                return null;

            }

            finally

            {

                con.Close();

                sda.Dispose();

                con.Dispose();

            }

        }

 

 

Once you execute this it will create folder hierarchy in your output folder with all documents.



Friday, June 1, 2012

Shortcut to "Save As Template" in SharePoint 2010


In SharePoint 2010, the shortcut of "Save As Template" option is URL /_layouts/SaveTmpl.aspx

Hide Left-hand Navigation in SharePoint 2010 Page Layout

In SharePoint Home page we don't need Left hand navigation, to hide that left hand navigation there is lots of way.

I found a simple way, just open your page layout in SharePoint designer and add following code.

<asp:Content ID="PageHead" ContentPlaceholderID="PlaceHolderAdditionalPageHead" runat="server">
<style type="text/css">
#s4-leftpanel
{
display:none;
}
.s4-ca
        {
         margin-left: 0px;
         background: transparent;
         }
</style>
</asp:Content>

It will hide Left hand Navigation from your Page layout.

Tuesday, April 17, 2012

Customize Send to in SharePoint 2010 Document Library

Create 2 web applications with a site collection of Team site Template
1. One  on port 8080
2. second on port 8081

 Now open the document library of site -8081 and go to settings. 






 Add details in Custom Send to destination
 Destination Name: - “Documents at 8080 Site”
URL =http://serverName:8080/Shared%20Documents/Forms/AllItems.aspx 





 




Now when you open the context menu of any document resides on document library (at port 8081)

 it shows you custom Send to option as shown in screen shot. 






 



When you click on Send to “Documents at 8080 Site”, it will show another site where you can change the name of file. 




 




Once you click OK it will transfer the file on site document library of site 8080


Thursday, March 22, 2012

Find Database Table's Foreign key mappings in SQL Server

The Foreign key mapping is stored in system database in SQL server, here is the query to find all the mappings.

Use <Database Name>
GO

SELECT f.name AS ForeignKey,
OBJECT_NAME(f.parent_object_id) AS TableName,
COL_NAME(fc.parent_object_id,
fc.parent_column_id) AS ColumnName,
OBJECT_NAME (f.referenced_object_id) AS ReferenceTableName,
COL_NAME(fc.referenced_object_id,
fc.referenced_column_id) AS ReferenceColumnName
FROM sys.foreign_keys AS f
INNER JOIN sys.foreign_key_columns AS fc
ON f.OBJECT_ID = fc.constraint_object_id



Find Database Table's Foreign key mappings in SQL Server

The Foreign key mapping is stored in system database in SQL server, here is the query to find all the mappings.

Use <Database Name>
GO

SELECT f.name AS ForeignKey,
OBJECT_NAME(f.parent_object_id) AS TableName,
COL_NAME(fc.parent_object_id,
fc.parent_column_id) AS ColumnName,
OBJECT_NAME (f.referenced_object_id) AS ReferenceTableName,
COL_NAME(fc.referenced_object_id,
fc.referenced_column_id) AS ReferenceColumnName
FROM sys.foreign_keys AS f
INNER JOIN sys.foreign_key_columns AS fc
ON f.OBJECT_ID = fc.constraint_object_id



Get Random string function for SQL

Most of the time we need a function in sql server to add random string in any field.

So I have created a function, which accepts the guid and generates the random string form that.



IF OBJECT_ID (N'dbo.get_RandomChar', N'FN') IS NOT NULL
    DROP FUNCTION dbo.get_RandomChar;
GO
-- Function to generate the random char, if the char is less then A then use A and if it is greater then z then use Z.
CREATE FUNCTION dbo.get_RandomChar(@newguid varchar(255))
RETURNS varChar(10)
WITH EXECUTE AS CALLER
AS
BEGIN
    declare @NewStr varchar(25)
set @NewStr =''
declare @Randomint int 
set @Randomint =ABS(CAST(@newguid AS binary(10)) %100)
-- not in A to Z and a to Z  _ - space and *
if not((@Randomint >= 65 and @Randomint <=90 ) or (@Randomint >= 97 and @Randomint <=122 ) or @Randomint=95 or @Randomint=45 or @Randomint =32 or @Randomint =42)
Begin
if (@Randomint <65)
set @Randomint =65
else
set @Randomint =122
End
set @NewStr =@NewStr+ convert(varchar,CHAR(@Randomint))
return(@NewStr);
END;
GO

-- function to create random string of 10 chars.
IF OBJECT_ID (N'dbo.get_RandomString', N'FN') IS NOT NULL
    DROP FUNCTION dbo.get_RandomString;
GO
CREATE FUNCTION dbo.get_RandomString(@newguid1 varchar(255),@newguid2 varchar(255),@newguid3 varchar(255),@newguid4 varchar(255),@newguid5 varchar(255),@newguid6 varchar(255),@newguid7 varchar(255),@newguid8 varchar(255),@newguid9 varchar(255),@newguid10 varchar(255))
RETURNS varChar(10)
WITH EXECUTE AS CALLER
AS
BEGIN
    return(dbo.get_RandomChar(@newguid1) +dbo.get_RandomChar(@newguid2)+dbo.get_RandomChar(@newguid3)+dbo.get_RandomChar(@newguid4)+dbo.get_RandomChar(@newguid5)+dbo.get_RandomChar(@newguid6)+dbo.get_RandomChar(@newguid7)+dbo.get_RandomChar(@newguid8)+dbo.get_RandomChar(@newguid9) +dbo.get_RandomChar(@newguid10));
END;
GO





to use this function call  like get_RandomString(NEWID(),NEWID(),NEWID(),NEWID(),NEWID(),NEWID(),NEWID(),NEWID(),NEWID(),NEWID()) 

most of the time when we do data scramble we need such function.




Get Random string function for SQL

Most of the time we need a function in sql server to add random string in any field.

So I have created a function, which accepts the guid and generates the random string form that.



IF OBJECT_ID (N'dbo.get_RandomChar', N'FN') IS NOT NULL
    DROP FUNCTION dbo.get_RandomChar;
GO
-- Function to generate the random char, if the char is less then A then use A and if it is greater then z then use Z.
CREATE FUNCTION dbo.get_RandomChar(@newguid varchar(255))
RETURNS varChar(10)
WITH EXECUTE AS CALLER
AS
BEGIN
    declare @NewStr varchar(25)
set @NewStr =''
declare @Randomint int 
set @Randomint =ABS(CAST(@newguid AS binary(10)) %100)
-- not in A to Z and a to Z  _ - space and *
if not((@Randomint >= 65 and @Randomint <=90 ) or (@Randomint >= 97 and @Randomint <=122 ) or @Randomint=95 or @Randomint=45 or @Randomint =32 or @Randomint =42)
Begin
if (@Randomint <65)
set @Randomint =65
else
set @Randomint =122
End
set @NewStr =@NewStr+ convert(varchar,CHAR(@Randomint))
return(@NewStr);
END;
GO

-- function to create random string of 10 chars.
IF OBJECT_ID (N'dbo.get_RandomString', N'FN') IS NOT NULL
    DROP FUNCTION dbo.get_RandomString;
GO
CREATE FUNCTION dbo.get_RandomString(@newguid1 varchar(255),@newguid2 varchar(255),@newguid3 varchar(255),@newguid4 varchar(255),@newguid5 varchar(255),@newguid6 varchar(255),@newguid7 varchar(255),@newguid8 varchar(255),@newguid9 varchar(255),@newguid10 varchar(255))
RETURNS varChar(10)
WITH EXECUTE AS CALLER
AS
BEGIN
    return(dbo.get_RandomChar(@newguid1) +dbo.get_RandomChar(@newguid2)+dbo.get_RandomChar(@newguid3)+dbo.get_RandomChar(@newguid4)+dbo.get_RandomChar(@newguid5)+dbo.get_RandomChar(@newguid6)+dbo.get_RandomChar(@newguid7)+dbo.get_RandomChar(@newguid8)+dbo.get_RandomChar(@newguid9) +dbo.get_RandomChar(@newguid10));
END;
GO





to use this function call  like get_RandomString(NEWID(),NEWID(),NEWID(),NEWID(),NEWID(),NEWID(),NEWID(),NEWID(),NEWID(),NEWID()) 

most of the time when we do data scramble we need such function.