Saturday, December 12, 2009

Uploading File to Document Library Using ASP.Net

1. Add reference to Microsoft.Sharepoint.Dll
2. [Code Behind ]

using (SPSite site = new SPSite("YourSite"))
{
using (SPWeb web = site.OpenWeb())
{
SPFolder folder = web.GetFolder(YourDocumentLibrary);
SPFileCollection files = folder.Files;
FileStream fStream = File.OpenRead(OfflineUploadCtrl.PostedFile.FileName);
byte[] contents = new byte[fStream.Length];
fStream.Read(contents, 0, (int)fStream.Length);
fStream.Close();
Hashtable MetaDataTable = new Hashtable();
MetaDataTable.Add("Comments", "MyFirstUpload");
SPFile currentFile = files.Add(YourDocLibraryPath + OfflineUploadCtrl.FileName, contents, MetaDataTable, true);
}
}

3. UI Code
<asp:FileUpload ID="OfflineUploadCtrl" runat="server" />
<asp:Button ID="UploadButton" runat="server" Text="Upload"
onclick="UploadButton_Click" />

Steps to create and deploy a custom web part using Asp.Net in Office Server 2007

Introduction

Office Server 2007 provides lot of out-of-box web parts to make development very easy to most of the scenarios. Still we may need to do some tailor-made solutions to meet our customer demands. We are going to see here how to create a custom web part and deploy the web part in our site and use it. Since the task of creation and deployment is important more than the sample it is up to the reader to put his own thoughts to that.
Web Part Creation

1. Create a web application
2. Add a user control to the web application
3. Add reference to Microsoft.Sharepoint.dll to the web application
4. Add a class file and do as mentioned below
[Code]
using System;
using System.Web.UI;

namespace YourNameSpace
{
public class YourClassName:Microsoft.SharePoint.WebPartPages.WebPart
{
#region "Global variable declarations"

protected Control _control = null;
protected string _exception = String.Empty;

//This UserControls is a folder in the VirtualServer
//directory of your Sharepoint WebApplication.
//We have to create this folder in the VirtualServer
//directory and copy the UserControl at that location
protected string UserControlPath = @"~/UserControls/";
protected string UserControlFileName = @"YourUserControl.ascx";

#endregion

#region "Create Child Controls method to create the controls for the webpart"

protected override void CreateChildControls()
{
try
{
_control = this.Page.LoadControl(UserControlPath + UserControlFileName);
this.Controls.Add(_control);

}
catch (Exception ex)
{
_exception += "CREATE ERROR:" + ex.Message;
}
finally
{
base.CreateChildControls();
}
}
#endregion

#region "RenderControls method , to render the controls inside the webpart class"

protected override void RenderContents(HtmlTextWriter writer)
{
try
{
base.RenderContents(writer);
}
catch (Exception ex)
{
_exception += "RENDER ERROR:" + ex.Message;
}
}

#endregion
}
}

[/Code]

5. Go to Solution Explorer -> Properties - > Assembly and add the following line as last
[assembly:System.Security.AllowPartiallyTrustedCallers]
6. Create an xml file with the name Manifest and add the following stuff

xmlns="http://schemas.microsoft.com/sharepoint/">










Change the SolutionId with a new GUID
7. Add a new file to the application with the name somename.ddf and add the following conent
;*** SharePoint Features Example MakeCAB Directive file
;
.OPTION EXPLICIT ; Generate errors
.Set CabinetNameTemplate=YourWSP.wsp
.set DiskDirectoryTemplate=CDROM ; All cabinets go in a single directory
.Set CompressionType=MSZIP ;** All files are compressed in cabinet files
.Set UniqueFiles="OFF"
.Set Cabinet=on
.Set DiskDirectory1=WSPFolder
;
; ** CAB Root
Manifest.xml
bin\YourWebApp.dll
;
;*** End

8. Add makecab.exe to the application
9. Open the command Prompt and execute the following
Makecab.exe –f Yourname.ddf
Web Part Deployment

10. For convenience open one more command prompt and execute the following
Stsadm –o addsolution filename [Your WSP Path ]\YourName.wsp
Changedirectory to C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\BIN
11. Go to Central Admin - > Operation - > Solution Management and follow the steps





Using the Web Part in the Site

12. Go to the site where you have deployed the webpart. Click SiteActions -> Modify all settings
13. Click Webparts [ in the gallery section]
14. In the webpart gallery -> select new -> select your webpart and click populate gallery.
15. Go to the page where you want to add the webpart.
16. Click SiteActions / Edit Page
17. Click the Add Webpart - > find your webpart in Mics zone and add.
There are two more things we need to do :
1. Go the site virtual directory and open the web.config file
And change the trust level from minimal to full
2. Create a new folder in the site virtual directory with the name what you have provided in the Step 4 and add your ascx file to this folder.

That’s it . You are good to go with your new web part