Sharepoint List Multiline text Filter web part

using System;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;

using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;

namespace ListFilter
{
[Guid("ee6429e6-80ea-40cf-a00d-fc9a76b69305")]
public class WebPart1 : System.Web.UI.WebControls.WebParts.WebPart
{
TextBox txtSearch;
Label lblmsg;
Button btnFilter;
public WebPart1()
{
}

protected override void CreateChildControls()
{
base.CreateChildControls();
lblmsg = new Label();
txtSearch = new TextBox();
btnFilter = new Button();
btnFilter.Text = “Filter”;
btnFilter.Click += new EventHandler(btnFilterClick);
this.Controls.Add(txtSearch);
//this.Controls.Add(new LiteralControl(“
“));
this.Controls.Add(btnFilter);
this.Controls.Add(new LiteralControl(“
“));
this.Controls.Add(lblmsg);

//load all the data
string oquery = “”;
try
{
SPContext.Current.Web.AllowUnsafeUpdates = true;

if (txtSearch.Text.Trim() != “”)
{
oquery = “”;
oquery += “” + txtSearch.Text.Trim() + “”;
oquery += “”;
}
Microsoft.SharePoint.SPViewCollection viewColl = SPContext.Current.Web.Lists["TestFilter"].Views;
for (int i = 0; i < viewColl.Count; i++)
{
Microsoft.SharePoint.SPView view = viewColl[i];
view.Query = oquery;
view.Update();
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
SPContext.Current.Web.AllowUnsafeUpdates = false;

txtSearch.Text = "";
}

}
protected void btnFilterClick(object sender, EventArgs e)
{

if (txtSearch.Text.Trim() == "")
{
lblmsg.Text = "You must specify a value for this required field";

string oquery = "";
try
{
SPContext.Current.Web.AllowUnsafeUpdates = true;

if (txtSearch.Text.Trim() != "")
{
oquery = "”;
oquery += “” + txtSearch.Text.Trim() + “”;
oquery += “”;
}
Microsoft.SharePoint.SPViewCollection viewColl = SPContext.Current.Web.Lists["TestFilter"].Views;
for (int i = 0; i < viewColl.Count; i++)
{
Microsoft.SharePoint.SPView view = viewColl[i];
view.Query = oquery;
view.Update();
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
SPContext.Current.Web.AllowUnsafeUpdates = false;

txtSearch.Text = "";
}
}
else
{

string oquery = "";
try
{
SPContext.Current.Web.AllowUnsafeUpdates = true;

if (txtSearch.Text.Trim() != "")
{
oquery = "”;
oquery += “” + txtSearch.Text.Trim() + “”;
oquery += “”;
}
Microsoft.SharePoint.SPViewCollection viewColl = SPContext.Current.Web.Lists["TestFilter"].Views;
for (int i = 0; i < viewColl.Count; i++)
{
Microsoft.SharePoint.SPView view = viewColl[i];
view.Query = oquery;
view.Update();
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
SPContext.Current.Web.AllowUnsafeUpdates = false;
txtSearch.Text = "";
}
}
}
}
}

Upload Document to Document library inside folder

public void UploadToMoss(string filename, int fileLength, Stream oStream, string strDocLibraryName, string atchType, string strETAID)
{
WindowsIdentity objWindowsIdentity = WindowsIdentity.GetCurrent();
WindowsImpersonationContext objWindowsImpersonationContext = null;
RevertToSelf();

SPSite siteCollection = null;
SPWeb topLevelSite = null;
SPList list = null;
SPFolderCollection folders = null;
SPFolder objFolder = null;
SPWeb test = null;

bool blStatus = true;
try
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
siteCollection = new SPSite(System.Configuration.ConfigurationManager.AppSettings["SPDocumentLibrary"].ToString());
topLevelSite = siteCollection.AllWebs[ConfigurationManager.AppSettings["TopLevelSite"].ToString()];
});
topLevelSite.AllowUnsafeUpdates = true;

list = topLevelSite.Lists[System.Configuration.ConfigurationManager.AppSettings["DocumentLibrary"]];
siteCollection.OpenWeb();

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

folders = topLevelSite.GetFolder(strDocLibraryName).SubFolders;
folders.Add(strETAID);

objFolder = topLevelSite.GetFolder(strDocLibraryName + “/” + strETAID);
byte[] bufDoc = null;
string strFileName = Path.GetFileName(filename);
int nLen = fileLength;
bufDoc = new byte[nLen];
oStream.Read(bufDoc, 0, nLen);

SPFile file = objFolder.Files.Add(strFileName, oStream, true);
file.Update();

bool blExists = doesSPWebCollectionContainName(folders, strETAID);

SPListItemCollection folderColl = list.Folders;

if (blExists)
{
SPFileCollection files = topLevelSite.Folders[strDocLibraryName].Files;
SPListItem item = file.Item;
item["ETAID"] = strETAID;
item["Attachment Type"] = atchType;
item.Update();
}

}
catch (Exception ex)
{
throw ex;
}
finally
{
topLevelSite.AllowUnsafeUpdates = false;
siteCollection.AllowUnsafeUpdates = false;
siteCollection.Dispose();
topLevelSite.Dispose();
list = null;
topLevelSite = null;
siteCollection = null;
folders = null;
objFolder = null;
}
}

Currency Convertion

function formatCurrency(num) {
num = num.toString().replace(/\$|\,/g,”);
if(isNaN(num))
num = “0″;
sign = (num == (num = Math.abs(num)));
num = Math.floor(num*100+0.50000000001);
cents = num%100;
num = Math.floor(num/100).toString();
if(cents<10)
cents = "0" + cents;
for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
num = num.substring(0,num.length-(4*i+3))+','+
num.substring(num.length-(4*i+3));
return (((sign)?'':'-') + '$' + num + '.' + cents);
}
———-

————–
input type=text name=input size=10 value=”1000434.23″>

———

GridView Radio Button Control

grdvwProjectNumber.DataSource = AccountingProject;
grdvwProjectNumber.DataBind();
FindChieldControls()
————————————————————
protected void FindChieldControls()
{
try
{
foreach (GridViewRow rowOne in grdvwProjectNumber.Rows)
{
string strParentProjID = string.Empty;
string strProjID = string.Empty;
Label lblProjectID = (Label)rowOne.FindControl(“lblProjectID”);
strProjID = lblProjectID.Text;
int intCount;
intCount = 0;
foreach (GridViewRow row in grdvwProjectNumber.Rows)
{
Label lblParentProjectID = (Label)row.FindControl(“lblParentProjectID”);
strParentProjID = lblParentProjectID.Text;
if (strParentProjID == strProjID)
{
intCount = intCount + 1;
break;
}
}
if (intCount != 0)
{
grdvwProjectNumber.Rows[rowOne.RowIndex].Cells[6].Controls.Clear();
}
}
}
catch (Exception ex)
{
GenericExceptionMethod(ex);
}
}

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();
}

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

private void updateSharePoint(NewECRET_SubmitBLLFields objNewECRET, string strNotifiers)
{ SPSite siteCollection = null;
SPWeb topLevelSite = null;
string strNewXRef = String.Empty;
bool blnNoError = true;

try
{

SPSecurity.RunWithElevatedPrivileges(delegate()
{
siteCollection = new SPSite(System.Configuration.ConfigurationManager.AppSettings["SPDocumentLibrary"].ToString());
topLevelSite = siteCollection.AllWebs[ConfigurationManager.AppSettings["TopLevelSite"].ToString()];
});
topLevelSite.AllowUnsafeUpdates = true;

SPList list = topLevelSite.Lists[ConfigurationManager.AppSettings["ListName"].ToString()];

// Create List Item
SPListItem objListItem = list.Items.Add();

objListItem["Title"] = objNewECRET.Title;
objListItem["ECRETS #"] = strRetECRETSID.ToString();

string strUserName = Session["FullName"].ToString();
for (int i = 0; i 0)
{
objListItem["Estimated Completion Date"] = txtEstimateComplitionDate.Text;
}
objListItem["Request Date"] = DateTime.Today.ToShortDateString().ToString();
objListItem["Timing"] = txtTiming.Text;
objListItem["Cost"] = txtCost.Text;
objListItem["How Affect Product"] = txtHowAffectProduct.Text;

string strNList = string.Empty;
if (lstnotification.Items.Count > 0)
{
for (int i = 0; i < lstnotification.Items.Count; i++)
{
if(!string.IsNullOrEmpty(lstnotification.Items[i].ToString()))
{
strNList = lstnotification.Items[i].ToString() + ";";
}
}
objNewECRET.NotificationList = strNList.ToString();
objListItem["NotificationList"] = strNList;
}

objListItem.Update();

topLevelSite.AllowUnsafeUpdates = false;

UploadECRETSFiles();
hdnDupTitle.Value = "Yes";
}
catch (Exception ex)
{
blnNoError = false;
lblMsg.Text = "Error " + ex.StackTrace.ToString() + " While Updating Sharepoint List";
// lblSuccessMsg.Text = "";
}
finally
{
if (blnNoError)
{
lblMsg.Text = "";
// lblSuccessMsg.Text = "Document has been reserved successfully";
txtComments.Enabled = false;
//btnSubmit.Enabled = false;
}
}
siteCollection.Dispose();
topLevelSite.Dispose();
if (blnNoError)
{

//Response.Redirect("Confirm.aspx?id=9&ECRETSID=" + strRetECRETSID);
spnECRETSID.InnerText = strRetECRETSID.ToString();
ScriptManager.RegisterStartupScript(this, Page.GetType(), "", "ShowDivLayer('shadow','divShowECRETSID');", true);

}
}

Load Attachments from List

public void ShowAttachment(string ListItemID, PlaceHolder PlaceHolderObj)
{

//Impersonating the user to perform this action
WindowsImpersonationContext ctx = null;
RevertToSelf();

SPSite siteCollection = null;
SPWeb topLevelSite = null;
SPList list = null;

string SPSiteCollection = string.Empty;
string SPTopLevelSite = string.Empty;
string SPListName = string.Empty;
string SPListSubFolderName = string.Empty;
string strSPListAttachmentLocation = string.Empty;
string strFileName = string.Empty;
int intID = 0;

SPSiteCollection = System.Configuration.ConfigurationManager.AppSettings["SPSite"];
SPTopLevelSite = System.Configuration.ConfigurationManager.AppSettings["TopLevelSite"];
SPListName = System.Configuration.ConfigurationManager.AppSettings["ListName"];
SPListSubFolderName = System.Configuration.ConfigurationManager.AppSettings["FolderName"];
strSPListAttachmentLocation = System.Configuration.ConfigurationManager.AppSettings["SPListAttachmentLocation"];

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

using (SPSite site = new SPSite(SPSiteCollection))
{

//using (SPWeb web = site.OpenWeb(SPTopLevelSite))
using (SPWeb web = site.AllWebs[SPTopLevelSite])
{
web.AllowUnsafeUpdates = true;

list = web.Lists[SPListName];

SPListItem item = list.GetItemById(Convert.ToInt32(ListItemID.ToString().Trim()));

SPAttachmentCollection attachments = item.Attachments;
if (attachments.Count > 0)
{
SPFolder folder = web.Folders["Lists"].SubFolders[SPListSubFolderName].SubFolders["Attachments"].SubFolders[item["ID"].ToString()];

foreach (SPFile objSPFile in folder.Files)
{
if (folder.Files.Count != 0)
{
strFileName = strSPListAttachmentLocation + objSPFile.ToString().Replace(” “, “%20″);
HyperLink objHyperLink = new HyperLink();
objHyperLink.NavigateUrl = strFileName.Trim();
objHyperLink.Text = objSPFile.Name.ToString();
objHyperLink.ID = intID.ToString();
intID++;

string strFileExt = String.Empty;
strFileExt = objSPFile.Name.ToString().Substring(objSPFile.Name.ToString().LastIndexOf(“.”) + 1);
if (strFileExt.ToUpper().Equals(“PDF”))
{
objHyperLink.Target = “_blank”;
}
PlaceHolderObj.Controls.Add(objHyperLink);
PlaceHolderObj.Controls.Add(new LiteralControl(“
“));
}
}
}
}
}
}

catch (Exception ex)
{
throw ex;
}
finally
{
if (topLevelSite != null)
{
topLevelSite.AllowUnsafeUpdates = false;
topLevelSite.Dispose();
}
if (siteCollection != null)
siteCollection.Dispose();

// Ensure impersonation is reverted
if (ctx != null)
ctx.Undo();
}
}

Update List Item in List using Object Model Code

public void AddChangeRequestFields(string strDocToRefer, string strUserFQN, string strMode)
{
SPSite siteCollection = null;
SPWeb topLevelSite = null;
string[] arrXRefDocs;
string strNewXRef = String.Empty;

siteCollection = new SPSite(System.Configuration.ConfigurationManager.AppSettings["SPDocumentLibrary"].ToString());
//topLevelSite = siteCollection.AllWebs["EPC"];
topLevelSite = siteCollection.AllWebs[ConfigurationSettings.AppSettings["TopLevelSite"].ToString()];

topLevelSite.AllowUnsafeUpdates = true;

SPList list = topLevelSite.Lists["Approved Documents"];

foreach (SPListItem item in list.Items)
{
if (strDocToRefer.Equals(Convert.ToString(item["Document Number"])))
{
if (strMode.Equals(“Add”))
{
item["Initiate Change Request"] = “”;
item["CR Initiated By"] = strUserFQN;
}
else
{
item["Initiate Change Request"] = System.Configuration.ConfigurationManager.AppSettings["SPDocumentLibrary"].ToString() + “?ID=” + strDocToRefer.Trim() + “&Type=CR”;
item["CR Initiated By"] = strUserFQN;
}

item.Update();
break;
}
}
topLevelSite.AllowUnsafeUpdates = false;

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

}

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();
}
}