Friday, March 12, 2010

Upload Word Document using Sharepoint Web Services


All,
Below is the sample code to add a Microsoft Word Document inside the Sharepoint Documen Library's folder.

Say i have a Document Library as CoastGuard and it has four folders : MIS,Aviation,IT and Logistics.So i need to add document in these folders using Sharepoint in built web services.

Below is the Code:

using System;

usingSystem.Collections.Generic;

using System.Configuration;

usingSystem.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using System.Xml;

using System.IO;

using System.Collections;

using Microsoft.SharePoint;

using System.Net;

namespace Signals

{

public partial class Form1:Form

{

public Form1()

{

InitializeComponent();

}

private voidForm1_Load(objectsender,EventArgs e)

{

GetFiles();

GetFoldersList();

}

private void GetFoldersList()

{

wsLists.Lists spList =newSignals.wsLists.Lists();

spList.Credentials = System.Net.CredentialCache.DefaultCredentials;

stringspUrl=ConfigurationSettings.AppSettings["SPFolders"].ToString();

spList.Url ="http://Win08r2/Signals/_vti_bin/Lists.asmx";

XmlNode listnodes = spList.GetListItems("CoastGuard", null, null, null, null, null,null);

foreach (XmlNode nodeinlistnodes)

{

if (node.Name == "rs:data")

{

for (int i = 0; i <>

{

if (node.ChildNodes[i].Name == "z:row")

{

listView1.Items.Add(node.ChildNodes[i].Attributes["ows_LinkFilename"].Value);

}

}

}

}

}

private void GetFiles()

{

string strPath ="..\\..\\Documents";

DirectoryInfo di =newDirectoryInfo(strPath);

FileInfo[] fi = di.GetFiles();

foreach (FileInfo f in fi)

{

listView2.Items.Add(f.Name);

}

}

privatevoidbutton1_Click(objectsender,EventArgs e)

{

wsLists.Lists spList =newSignals.wsLists.Lists();

spList.Credentials = System.Net.CredentialCache.DefaultCredentials;

spList.Url ="http://Win08r2/_vti_bin/Lists.asmx";

listView1.Items[0].Selected =true;

string DocFileName = "";

foreach (ListViewItem linlistView2.SelectedItems)

{

DocFileName = l.Text.ToString();

}

if (DocFileName != "")

{

if(listView1.CheckedItems.Count > 0)

{

for (int i = 0; i <>

{

WebClient WC =newWebClient();

WC.Credentials =CredentialCache.DefaultCredentials;

WC.UploadData("http://Win08r2/signals/CoastGuard/"+ listView1.CheckedItems[i].Text + "/" + DocFileName, "PUT", StreamFile(@"..\\..\\Documents\" + DocFileName));

}

MessageBox.Show("Files moved successfully");

}

else

{

MessageBox.Show("Please Select At Least One Folder");

}

}

else

{

MessageBox.Show("Please Select a File");

}

}

private byte[] StreamFile(stringfilename)

{

FileStream fs =newFileStream(filename,FileMode.Open,FileAccess.Read);

// Create a byte array of file stream length

byte[] ImageData =newbyte[fs.Length];

//Read block of bytes from stream into the byte array

fs.Read(ImageData, 0, System.Convert.ToInt32(fs.Length));

//Close the File Stream

fs.Close();

return ImageData; //return the byte data

}

privatevoidbutton2_Click(objectsender,EventArgs e)

{

foreach (ListViewItem liinlistView1.Items)

{

li.Checked = true;

}

}

privatevoidbutton3_Click(objectsender,EventArgs e)

{

foreach (ListViewItem liinlistView1.Items)

{

li.Checked = false;

}

}

}

}

Here's the Result:

Form1 screenshot.Multiple folders can be selected,but only single file can be selected.


Document uploaded successfully..



Regards,

Nitin Sharma