Archive

Archive for September, 2009

Load Attachments From List Library-MOSS 2007


using Microsoft.SharePoint;
using System.Security.Permissions;
using System.Security.Principal;
using System.Runtime.InteropServices;
———————————————————————
if (!IsPostBack)
{
objCommonMethods.ShowAttachment(Session[“TRANSID”].ToString(), phAttactments);
if (phAttactments.Controls.Count == 0)
{
lblNoAttachments.Visible = true;
lblNoAttachments.Text = “None Available”;
}

//Get Attachments to Hidden field
objCommonMethods.GetAttachmentsInfo(hdnTxtAttachments, phAttactments);

}
else
{
objCommonMethods.ReloadAttachments(hdnTxtAttachments.Value.Trim(), phAttactments);
//AttchmentsUpload();
}
——————————————————————————————————

public void ShowAttachment(string strTransactionID, PlaceHolder PlaceHolderObj)
{
WindowsIdentity wi = WindowsIdentity.GetCurrent();
WindowsImpersonationContext ctx = null;
RevertToSelf();

SPSite siteCollection = null;
SPWeb objSPWeb = null;
SPList list = null;
string fileName = string.Empty;
int intID = 0;

string strSPListAttachmentLocation = string.Empty;

try
{

ctx = WindowsIdentity.GetCurrent().Impersonate();
strSPListAttachmentLocation = System.Configuration.ConfigurationManager.AppSettings[“SPListAttachmentLocation”];
siteCollection = new SPSite(System.Configuration.ConfigurationManager.AppSettings[“SPDocumentLibrary”]);

//objSPWeb = siteCollection.RootWeb;
objSPWeb = siteCollection.AllWebs[System.Configuration.ConfigurationManager.AppSettings[“TopLevelSite”]];
list = objSPWeb.Lists[System.Configuration.ConfigurationManager.AppSettings[“ListName”]];

siteCollection.OpenWeb();

objSPWeb.AllowUnsafeUpdates = true;

foreach (SPListItem objSPListItem in list.Items)
{

if (strTransactionID.Equals(Convert.ToString(objSPListItem[“TransactionID”]).Trim()))
{

string strListID = objSPListItem[“ID”].ToString();
bool boolFilesExists = false;
int intFolders = objSPWeb.Folders[“Lists”].SubFolders[System.Configuration.ConfigurationManager.AppSettings[“FolderName”]].SubFolders[“Attachments”].SubFolders.Count;

//Before creating the spfolder object it checks whether the folder exists or not
for (int index = 0; index < intFolders; index++)
{
if (objSPWeb.Folders["Lists"].SubFolders[System.Configuration.ConfigurationManager.AppSettings["FolderName"]].SubFolders["Attachments"].SubFolders[index].Name.Equals(strListID))
{
boolFilesExists = true;
break;
}
}

if (boolFilesExists)
{
SPFolder folder = objSPWeb.Folders["Lists"].SubFolders[System.Configuration.ConfigurationManager.AppSettings["FolderName"]].SubFolders["Attachments"].SubFolders[objSPListItem["ID"].ToString()];

foreach (SPFile file in folder.Files)
{
if (folder.Files.Count != 0)
{
fileName = strSPListAttachmentLocation + file.ToString().Replace(" ", "%20");
HyperLink hpl = new HyperLink();
hpl.NavigateUrl = fileName.Trim();
hpl.Text = file.Name.ToString();
hpl.ID = intID.ToString();
intID++;
//hpl.Attributes.Add("onclick", "javascript:window.open(" + hpl.NavigateUrl + ");");
PlaceHolderObj.Controls.Add(hpl);
PlaceHolderObj.Controls.Add(new LiteralControl("
“));
}
}
break;
}

}
}

}
catch (Exception ex)
{
throw ex;
}
finally
{
objSPWeb.AllowUnsafeUpdates = false;
siteCollection.Dispose();
objSPWeb.Dispose();
ctx.Undo();
}
}
———————————————————————————————–

public void GetAttachmentsInfo(HtmlInputHidden hdbTxtAttachments, PlaceHolder phAttachments)
{
for (int i = 0; i < (phAttachments.Controls.Count / 2); i++)
{
HyperLink hl = null;
hl = (HyperLink)phAttachments.FindControl(i.ToString());

hdbTxtAttachments.Value += hl.Text + "?" + hl.NavigateUrl + ";";
}
}
————————————————————————————————

public void ReloadAttachments(string strTxtAttachments, PlaceHolder phAttachments)
{
if (strTxtAttachments != "")
{
string[] strFiles = strTxtAttachments.Split(';');
for (int i = 0; i < strFiles.Length – 1; i++)
{
HyperLink hl = new HyperLink();
hl.Text = strFiles[i].ToString().Substring(0, strFiles[i].ToString().LastIndexOf("?"));
hl.ID = i.ToString();
hl.NavigateUrl = strFiles[i].ToString().Substring(strFiles[i].ToString().LastIndexOf("?") + 1);

phAttachments.Controls.Add(hl);
phAttachments.Controls.Add(new LiteralControl("
“));
}
}
}

SQL Server forgot password

Categories: MOSS 2007