Archive

Archive for November, 2009

Load attachments from Document library to .Net


public void ShowAttachment(PlaceHolder PlaceHolderObj, string strListItemID)

{

WindowsIdentity objWindowsIdentity = WindowsIdentity.GetCurrent();

WindowsImpersonationContext objWindowsImpersonationContext = null;

 

//RevertToSelf();

SPSite siteCollection = null;

SPWeb topLevelSite = null;

 

string strDocumentLink = String.Empty;

string strDocLibraryWebLink = System.Configuration.ConfigurationManager.AppSettings[“SPDocumentLibraryWeb”].ToString();

string strDocLibraryLink = System.Configuration.ConfigurationManager.AppSettings[“SPDocumentLibrary”].ToString();

string strDocLibrary = System.Configuration.ConfigurationManager.AppSettings[“SPDocLibrary”].ToString();

objWindowsImpersonationContext = WindowsIdentity.GetCurrent().Impersonate();

siteCollection = new SPSite(strDocLibraryLink);

topLevelSite = siteCollection.AllWebs[System.Configuration.ConfigurationManager.AppSettings[“TopLevelSite”].ToString()];

SPList list = topLevelSite.Lists[“ETAAttachments”];

 

 

 

foreach (SPListItem item in list.Items)

{

 

if ((Convert.ToString(item[“ETAID”]).Equals(strListItemID)))

{

strDocumentLink = strDocLibraryWebLink + strDocLibrary + “/” + Convert.ToString(item[“Name”]);

 

HyperLink hpl = new HyperLink();

hpl.NavigateUrl = strDocumentLink.ToString().Replace(” “, “%20”).Trim();

 

hpl.Text = Convert.ToString(item[“Name”]);

hpl.Attributes.Add(“onclick”, “javascript:window.open(” + hpl.NavigateUrl +”);”);

 

PlaceHolderObj.Controls.Add(hpl);

PlaceHolderObj.Controls.Add(new LiteralControl(“”));

}

}

objWindowsImpersonationContext.Undo();

}

http://www.infoitweb.com/Common/Articles.aspx?TypeID=11&ContentID=138

Add new row (With Document) in Document libraray using Object Model Code


public void UploadToMoss(string filename, int fileLength, Stream oStream, string strDocLibraryName)
{
WindowsIdentity wi = WindowsIdentity.GetCurrent();
WindowsImpersonationContext ctx = null;
//RevertToSelf();

bool blStatus = true;
try
{

ctx = WindowsIdentity.GetCurrent().Impersonate();

SPSite siteCollection = null;
SPWeb topLevelSite = null;
string strDocLibraryLink = System.Configuration.ConfigurationManager.AppSettings[“SPDocumentLibrary”].ToString();

siteCollection = new SPSite(strDocLibraryLink);
topLevelSite = siteCollection.AllWebs[ConfigurationManager.AppSettings[“TopLevelSite”].ToString()];

topLevelSite.AllowUnsafeUpdates = true;
SPFolder objFolder = topLevelSite.GetFolder(strDocLibraryName);
byte[] bufDoc = null;
string strFileName = Path.GetFileName(filename);
int nLen = fileLength;
bufDoc = new byte[nLen];
oStream.Read(bufDoc, 0, nLen);

SPList list = topLevelSite.Lists[“ETAAttachments”];

topLevelSite.AllowUnsafeUpdates = false;

System.GC.AddMemoryPressure(200024);
SPFile file = objFolder.Files.Add(strFileName, oStream, true);
file.Update();

foreach (SPListItem item in list.Items)
{

if (item[“Name”].ToString() == strFileName)
{

item[“ETAID”] = 278;
item[“Attachment Type”] = “test”;
item.Update();
break;
}

}

siteCollection.Dispose();
topLevelSite.Dispose();

}
catch (Exception ex)
{
throw;
}
finally
{
System.GC.RemoveMemoryPressure(200024);
ctx.Undo();
}
}

Add new list row using Object Model is sharepoint list

Load Attachments from List

Update SharePoint List Item using CSOM

Move Document


Move Document From One Folder to Another Folder In Doc Library
—————————————————————-
public void MoveDocument(string strSourceDocumentLibrary, string strDestinationDocLibrary, string strDocument)
{
WindowsIdentity wi = WindowsIdentity.GetCurrent();
WindowsImpersonationContext ctx = null;
RevertToSelf();
string strDestinationURL = String.Empty;
try
{
ctx = WindowsIdentity.GetCurrent().Impersonate();

SPSite siteCollection = null;
SPWeb topLevelSite = new SPSite(System.Configuration.ConfigurationManager.AppSettings[“SPDocumentLibraryWeb”].ToString()).OpenWeb();

string strRevFileName = String.Empty;
string strFileNameWOExt = String.Empty;
string strFileExt = String.Empty;

siteCollection = new SPSite(System.Configuration.ConfigurationManager.AppSettings[“SPDocumentLibrary”]);
//topLevelSite = siteCollection.AllWebs[“fsdocs”];

siteCollection.AllowUnsafeUpdates = true;
topLevelSite.AllowUnsafeUpdates = true;

SPList list = topLevelSite.Lists[“Draft Documents”];

//strDestinationURL = System.Configuration.ConfigurationManager.AppSettings[“SPDocumentLibrary”].ToString() + “/epcdocs” + “/” + strDestinationDocLibrary;
strDestinationURL = System.Configuration.ConfigurationManager.AppSettings[“SPDocumentLibraryWeb”].ToString() + “/” + strDestinationDocLibrary;

foreach (SPListItem item in list.Items)
{

//if (Convert.ToString(item[“Document Filename”]).Equals(strDocument))

if (Convert.ToString(item[“Name”]).Equals(strDocument))
{
//SPFile SPFileName = topLevelSite.GetFile(System.Configuration.ConfigurationManager.AppSettings[“SPDocumentLibrary”].ToString() + “/epcdocs/” + strSourceDocumentLibrary + “/” + Convert.ToString(item[“Document Filename”]));
SPFile SPFileName = topLevelSite.GetFile(System.Configuration.ConfigurationManager.AppSettings[“SPDocumentLibraryWeb”].ToString() + “” + strSourceDocumentLibrary + “/” + Convert.ToString(item[“Name”]));

strFileNameWOExt = SPFileName.Name.ToString().Substring(0, SPFileName.Name.ToString().LastIndexOf(“.”));
strFileExt = SPFileName.Name.ToString().Substring(SPFileName.Name.ToString().LastIndexOf(“.”));

SPFileName.CopyTo(strDestinationURL + “/” + strFileNameWOExt.Trim() + strFileExt, true);

SPFileName.Delete();
break;
}
}
siteCollection.AllowUnsafeUpdates = false;
topLevelSite.AllowUnsafeUpdates = false;

siteCollection.Dispose();
topLevelSite.Dispose();
}
catch (Exception ex)
{
throw;// new Exception(“While uploading document to Draft… (Changed) ” + strDestinationURL);
}
finally
{
ctx.Undo();
}
}

Sharepoint Upload Document to Document Library