About Me

My photo
Muthupet, TamilNadu, India
SharePoint 2010

Wednesday, December 18, 2013

How to print data from gridview control using asp.net with c#



hi,
try this code

Javascript function to print.
<script language="javascript" type="text/javascript">
    function CallPrint()
    {
        var prtContent = document.getElementById("divPrint");
        var WinPrint = window.open('','','letf=0,top=0,toolbar=0,scrollbars=0,status=0');
        WinPrint.document.write(prtContent.innerHTML);
        WinPrint.document.close();
        WinPrint.focus();
        WinPrint.print();
        WinPrint.close();
    }
</script>

Print Button
<asp:Button ID="btnprint" runat="server" Text="Print" OnClientClick="javascript:CallPrint();" />

GridView to Print
<div id="divPrint" style="text-align: center; width: 100%">
    <asp:GridView ID="GView" runat="server">
        <Columns>
.....
</Columns>
    </asp:GridView>
</div>

Saturday, September 21, 2013

Uploading Documents Programatically to a Content Organizer Enabled Site

We have covered Content Organizer / Metadata Routing in SP2010 in earlier post.
We have created all the required infrastructure for meta data routing or Content organizing. So only task remining is uploading the documents.
Well ! we cannot ask the users to navigate to Drop Off library and upload their documents. Right?
So i have created a simple Webpart for uploading the documents for the specific content type.
 Now we need to upload the selected files programatically to Drop Off Library.
Below is the code written by me for uplaoding the file to DropOff Library of the respective site.
 
private void UploadFromFileControl(FileUpload fuFile,string description)
        {
            using (SPWeb objWeb = SPContext.Current.Site.OpenWeb())
            {
                objWeb.AllowUnsafeUpdates = true;
                
                SPFolder myLibrary = objWeb.Folders["DropOffLibrary"];
                Boolean replaceExistingFiles = true;
                String fileName = System.IO.Path.GetFileName(fuFile.PostedFile.FileName);
                Int32 fileLen = fuFile.PostedFile.ContentLength;
                Byte[] Input = new Byte[fileLen];
                Stream fileStream = fuFile.FileContent;
                fileStream.Read(Input, 0, fileLen);
                SPFile spFile = myLibrary.Files.Add(Path.GetFileName(fuFile.PostedFile.FileName), fileStream, replaceExistingFiles);

                SPListItem spListItem = spFile.ListItemAllFields;
                spListItem["ContentType"] = objWeb.ContentTypes["CntUploadedDocs"];
                spListItem["CandidateId"] = CandidateId;
                spListItem["Relationship"] = ddlRelation.Items[ddlRelation.SelectedIndex].Value.ToString();
                spListItem["DocumentType"] = ddlDocumentType.Items[ddlDocumentType.SelectedIndex].Value.ToString();
                spListItem["Description"] = description;
                spListItem.Update();
                spFile.Update();
            }
        }
Result: Code executed perfectly, and the Document is uploaded to DropOff Library. I opened the DropOff  library and the record is successfully added. Task Acomplished?

No! Our task is not to add the document to DropOff library. Automatically it has to be moved from DropOff library to destination Library by Content organizer. But why it is still waiting there. Then i came across a stunning fact that . . .
Content Organizer Rules will not be triggered for Documents added to DropOff Library Programatically
Now i went to Content Organizer settings and copied the webservice Url at the bottom.
Usually it ends with "/vti_bin/OfficialFile.asmx".
Reffered the web service in my visual studio solution with name "FileUploadSvc".
Obviously you will end up writing code using "SPFile", "SPListItem" and "FileUploadSvcSoapClient".
It is a tedious process of doing so, but later i realized that reffering OfficialFile.asmx will create a Circualr refrence issue.
Reason being, the web service url has to be used for configuring  "SendTo" connections in Central Admin for access from other sites. Not for refering in your own website.

Now, i end up with a big question . .

How to Upload Documents to Content Organizer Enabled Site Programtically and make the Content Oragnizer rule trigger automatically?
Here is the answer . . . Microsoft.Office.Policy.dll 
Add this dll to your solution, and use "Microsoft.Office.RecordsManagement.RecordsRepository.dll" in your class file.

Doing so, you can access a class called "OfficialFileCore"  which will reffer the OfficialFile.asmx webservice by default. Try browsing the webservice and see what are the methods available.

Look at th last method, "SubmitFile". That is the method we need to use and MSDN has lot of information to offer about syntax and other stuff.
Microsoft link talking about Syntax.
Now i changed the code of my usercontrol to :
        
private void UploadFromFileControl(FileUpload fuFile,string description)
        {
            using (SPWeb objWeb = SPContext.Current.Site.OpenWeb())
            {               
                try
                {
                    string destination = null;
                    String fileName = System.IO.Path.GetFileName(fuFile.PostedFile.FileName);
                    Int32 fileLen = fuFile.PostedFile.ContentLength;
                    Byte[] Input = new Byte[fileLen];
                    Stream fileStream = fuFile.FileContent;
                    fileStream.Read(Input, 0, fileLen);

                    List properties = new List();
    properties.Add(new Microsoft.SharePoint.RecordsRepositoryProperty() 
              { Name = "Name", Type = "Text", Value = fileName });
    properties.Add(new Microsoft.SharePoint.RecordsRepositoryProperty() 
              { Name = "CandidateId", Type = "Text", Value = CandidateId });
    properties.Add(new Microsoft.SharePoint.RecordsRepositoryProperty() 
              { Name = "Relationship", Type = "Text", Value = ddlRelation.Items[ddlRelation.SelectedIndex].Value.ToString() });
    properties.Add(new Microsoft.SharePoint.RecordsRepositoryProperty() 
              { Name = "DocumentType", Type = "Text", Value = ddlDocumentType.Items[ddlDocumentType.SelectedIndex].Value.ToString() });
   properties.Add(new Microsoft.SharePoint.RecordsRepositoryProperty() 
              { Name = "Description", Type = "Text", Value = description });
   properties.Add(new Microsoft.SharePoint.RecordsRepositoryProperty() 
              { Name = "ContentType", Type = "Text", Value = "CntUploadedDocs" });
   var result = OfficialFileCore.SubmitFile(objWeb, Input, properties.ToArray(), "CntUploadedDocs", fuFile.PostedFile.FileName, @"domain\pratap", false, out destination);

                }
                catch (Exception ex)
                {
                }
                
            }
        }

Before deploying the code, there is another step to perform to have a smooth execution.
Go to Site Actions > Site Permissions > Add the Current or Configured user  to this group.

If you havent done above action, you will not be able to add the records to any library and the SubmitFile method will return "NotFound" response back as a result of execution.

Deployed the solution and added the record again.
DropOff Library:
Destination Document Library:

Task Acomplished. We are able to Add / Upload document programatically to a Content Organizer enabled site.
Is it helpful for you? Kindly let me know your comments / Questions.

Thursday, July 25, 2013

Change “SharePoint” Text in Top Left to Company Logo


By now you have noticed the “SharePoint” text in the top left corner on SharePoint 2013. What if you would like to replace this with your own text, or better yet a clickable image of your company logo? Once again PowerShell is here to help you. Note that this is a Web Application level change and will affect all sites within the Web Application.

Here is our starting point

Steps to change

Open the SharePoint 2013 Management Shell and let’s look at the default setting:
1
2
$webApp = Get-SPWebApplication http://intranet.orion.com
$webApp.SuiteBarBrandingElementHtml
You can see that it’s simply taking in HTML to render the text. So, here is the HTML that I’ll use to display our company logo linked to the root Site Collection:
1
2
3
$webApp = Get-SPWebApplication http://intranet.orion.com
$webApp.SuiteBarBrandingElementHtml = '<div class="ms-core-brandingText"><a href="http://intranet.orion.com"><img src="http://intranet.orion.com/SiteAssets/logo_contoso.png"/></a></div>'
$webApp.Update()

The result

Pretty simple, eh?

Here is the default setting should you need to revert back:

1
2
3
$webApp = Get-SPWebApplication http://intranet.orion.com
$webApp.SuiteBarBrandingElementHtml = '<div class="ms-core-brandingText">SharePoint</div>'
$webApp.Update()

Remove SharePoint 2013 MaterPage in Dialog Box

Apply the following css to your master page css.

html.ms-dialog div.s4-workspace{display:none;}
html.ms-dialog div.a{display:none;}
html.ms-dialog div.b{display:none;}
html.ms-dialog div.search{display:none;}

create a custom Page Layout in share point 2013

With the new Designer Manager in SharePoint 2013 it is now much easier to create a custom Page Layout. In this blog article I ‘m providing a step by step pictorial guidance on how to create a simple custom Page Layout.
My prerequisites: I have created a site collection based on the template ‘Publishing Portal’ and installed SharePoint Designer 2013.
image
What I need is a simple Page Layout with two columns with different widths. These columns should be created as Web Part Zones to enable editors to place Web Parts on the page.
First I open the site settings and start the new Design Manager:
image
Because I want to create a custom Page Layout I click on ‘Edit Page Layouts’ …
image
… and create a new Page Layout (based on a predefined master page and the ‘Article Page’ content type):
image
It should take just a few seconds until SharePoint has created the new Page Layout.
image
OK - now let’s switch over to SharePoint Designer 2013. To find the newly created Page Layout I click on ‘All Files’ in the left navigation. Now I open the folder _catalogs and click on master page. I have marked the files of the newly created Page Layout with a red rectangle.
image
The newly created Page Layout consists of two files. Don’t touch the aspx-file! To create a custom Page Layout you only need to edit the html-file. SharePoint will create and update the aspx-file based on the changes in the associated html-file.
Let’s open the html-file to see what SharePoint has created. I check out the html-file and open it in Advanced Mode:
Now this is what you should see:
image
For this demo I have chosen the Article Page as the template for my new Page Layout - but I don’t want to keep most of the controls. So I scroll down to the area of the html-file where the page controls are defined.
image
The page controls are surrounded by <div> tags - so they should be pretty easy to find. Now I locate this line of code:
<!--MS:<asp:ContentPlaceHolder ID="PlaceHolderMain" runat="server">-->
and delete everything beginning from (but excluding) the line above until (excluding) this line:
<!--ME:</asp:ContentPlaceHolder>-->
In other words: I delete every line of code between these two (ContentPlaceHolder) lines (but I keep these lines).
With this deletion I have removed almost every page control and the newly created custom Page Layout file should look like this (I marked the deleted area with an empty red box):
image
Let’s see how this empty Page Layout looks like in SharePoint 2013. I save and check-in the html-file in SharePoint Designer 2013 and switch back to SharePoint 2013. Have look on the fifth screenshot again - here I click on the name of my newly created Page Layout and SharePoint 2013 opens this page:
image
Looks like a pretty empty page! To better check the changes in my custom Page Layout I create another page that uses my newly created custom Page Layout.
This is what the newly created page based on my custom Page Layout now looks like:
image
This page is empty in edit mode too, because I just removed all page controls. Now it’s time to add two Web Part zones..
I switch back to SharePoint Designer 2013 and open the html-file in Advanced Mode again. In the gap where I deleted the page controls before I now add a simple html table like this:
image
Just a simple table with one row and two columns (in other words: two parallel table cells). The first column should have a width of 80% and the second one should have a width of 20%. Both columns should be top-aligned.
The next step is: I need to add two Web Part Zones: one in each table’s column. I save the changes in SharePoint Designer 2013 and switch back to SharePoint’s Design Manager.
I open this page …
image
… and click on Snippets:
Now the Snippets Designer is shown and I click on Web Part Zone:
image
The Snippets Designer creates the HTML code for a new Web Part Zone and I can copy the HTML code snippet to the clipboard. You can customize some of the parameters of the Web Part Zone (or any other snippet) before copying the snippet to the clipboard. To edit parameters click on the greyed parameter names on the right.
image
Now I jump back to SharePoint Designer, open the html-file again and paste the snippet to the first table column. After that I switch back to the Snippets Designer and create another (a new - that’s important) Web Part Zone snippet with a new ID (!) and copy the snippet’s code to the second table column.
My html-file looks like this now:
image
Did you notice the doubled Page Title in the 10th screenshot? Doesn’t look very nice - that’s why  I remove the placeholder with the ID PlaceHolderPageTitleInTitleArea. I don’t want the page title here - for my demo the name of the aspx-file is sufficient. I save the changes in SharePoint Designer 2013 and check-in the html-file again.
Let’s see how the newly created Page Layout looks like now. I switch back to SharePoint 2013 and open the site with the newly created Demo Page … and hit F5 to refresh the page:
image
There are now two Web Part Zones with a different width and both are top-aligned! Just to prove that this Page Layout is working as expected I add some Web Parts to the two zones and publish the page:
image
That’s how I wanted the custom Page Layout to look like. If you want to create a more detailed Page Layout just have a look at the snippets. There are several useful snippets you can use in your own custom Page Layout by simply getting the HTML code of the snippet and pasting it to the custom Page Layout.
image
Compared to former version of SharePoint creating custom Page Layouts has been simplified very much. The new Design Manager in SharePoint 2013  (together with the Snippets) makes it easier to create custom Page Layouts. Use this blog article as a short introduction and feel free to experiment with Page Content Types, snippets and custom Page Layouts on your own.