Friday, July 29, 2011

Creating Modal Dialog in Sharepoint 2010 using Visual Studio 2010

This article will explain how to create a modal dialog using Visual studio 2010.

Open visual studio 2010 and create Empty Sharepoint project. Name it as ‘ModalDialog’. Select Deploy as farm solutions option.

Right click on ‘ModalDialog’ project at the Solution explorer, Add New Item.

Select VisualWebPart template and name it as ‘Upload’.

Add the following script to the Upload user control.

<script type="text/javascript">

function OpenWebPage() {

var options = {

url: "http://sharepoint-pc:3395/sites/mcts/SitePages/TestModal.aspx",

width: 400,

height: 100,

dialogReturnValueCallback: DialogCallback

};

SP.UI.ModalDialog.showModalDialog(options);

}

function DialogCallback(dialogResult, returnValue) {

meesageId = SP.UI.Notify.addNotification(returnValue, false);

}

script>

For the url at the above script insert the url you want to show in the modal dialog.

Under the script> add a button to do the action.

<input type="button" value="Upload" id="btnUpload" onclick="OpenWebPage();"/>

Build the solution and Deploy.

You will find the web part under Custom category.

Add the Web part to a web page and test it!!

Monday, July 25, 2011

Programmatically upload documents in SharePoint 2010

Programmatically upload documents in SharePoint 2010

This article is about uploading documents to a Document library in Sharepoint 2010 using C# code.

Steps:

Open Visual studio 2010, Create new Sharepoint 2010 project. Select VisualWebPart template and name it as ‘DocumentUpload


Mention the site name and select ‘Deploy as a farm solution’ option.

Rename ‘VisualWebPart1’ as ‘DocumentUploadWebPart’.

Open’ DocumentUploadWebPart’ in design mode. Insert ‘FileUpload’ and ‘Button’ controls to the webpart. Select Button and go to properties. Change ID to btnUpload and Text to Upload.

At the solution explorer add the following assemblies to the Reference.

  • · Microsoft.SharePoint.Client.Runtime.dll
  • · Microsoft.SharePoint.Client.dll

Double click on Upload button to go to code view.

Include the following reference at the top.

using SP = Microsoft.SharePoint.Client;

Insert the following code to the Upload button click event

protected void btnUpload_Click(object sender, EventArgs e)

{

string library = "Shared Documents";

SP.ClientContext ctx = new SP.ClientContext(@"http://sharepoint-pc:3395/sites/mcts");

var web = ctx.Web;

var fciNewFileFromComputer = new SP.FileCreationInformation();

fciNewFileFromComputer.Content = System.IO.File.ReadAllBytes(FileUpload1.PostedFile.FileName);

fciNewFileFromComputer.Url = System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName);

SP.List docs = web.Lists.GetByTitle(library);

SP.File uploadedFile = docs.RootFolder.Files.Add(fciNewFileFromComputer);

ctx.ExecuteQuery();

}

Build and deploy the feature. You will find this web part under Custom category when you insert webparts to the pages.

Thats it!!