Search This Blog

Monday, December 27, 2010

How to copy attachments from one list item to another

private void CopyAttachments(SPListItem sourceItem, SPListItem targetItem)
{
try
{
//get the folder with the attachments for the source item
SPFolder sourceItemAttachmentsFolder =
sourceItem.Web.Folders["Lists"].SubFolders[sourceItem.ParentList.Title].SubFolders["Attachments"].SubFolders[sourceItem.ID.ToString()];
//Loop over the attachments, and add them to the target item
foreach (SPFile file in sourceItemAttachmentsFolder.Files)
{
byte[] binFile = file.OpenBinary();
targetItem.Attachments.AddNow(file.Name, binFile);
}
}
catch { }
finally
{
sourceItem.Web.Dispose();
}
}

Accordion

<style>
.accordionHeaderWO
{
border: 1px solid #2F4F4F;
color:#FFF;
background-color: #7D1514;
font-size: 12px;
padding: 5px;
margin-top: 5px;
cursor: pointer;
}
.accordionContent
{
background-color: White;
padding: 5px;
padding-top: 10px;
}
.accordionLink
{
background-color: #D3DEEF;
color: white;
}
<style>

----------------------
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div>
<cc1:Accordion ID="Accordion1" runat="server" SelectedIndex="0" HeaderCssClass="accordionHeaderWO"
ContentCssClass="accordionContent" FadeTransitions="true" TransitionDuration="250"
FramesPerSecond="40" AutoSize="None" RequireOpenedPane="true">
<Panes>
<cc1:AccordionPane ID="AccordionPane1" runat="server">
<Header> Rejected Vendor Information</Header>
<Content>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
</Content>
</cc1:AccordionPane>

<cc1:AccordionPane ID="AccordionPane2" runat="server">
<Header>
Comments
</Header>
<Content>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</Content>
</cc1:AccordionPane>

</Panes>
</cc1:Accordion>
</div>
</form>

EncryptionHelper

public class EncryptionHelper
{

# region Function to encrypt QueryString
public static string EncryptText(string pass)
{
return Encrypt(pass, "&%#@?,:*");
}
#endregion

#region EncryptQuery string
///
/// Encrypts a particular string with a specific Key
///

///
///
///

public static string Encrypt(string stringToEncrypt, string encryptionKey)
{
byte[] key = { };
byte[] IV = { 10, 20, 30, 40, 50, 60, 70, 80 };
byte[] inputByteArray; //Convert.ToByte(stringToEncrypt.Length)

try
{
key = Encoding.UTF8.GetBytes(encryptionKey.Substring(0, 8));
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
inputByteArray = Encoding.UTF8.GetBytes(stringToEncrypt);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(key, IV), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
return Convert.ToBase64String(ms.ToArray());
}
catch (System.Exception ex)
{
return (string.Empty);
}
}
#endregion

#region Function decrypt Querystring
public static string DecryptText(String pass)
{
return Decrypt(pass, "&%#@?,:*");
}
#endregion

#region Decrypt QueryString
///
/// Decrypts a particular string with a specific Key
///

public static string Decrypt(string stringToDecrypt, string sEncryptionKey)
{
byte[] key = { };
byte[] IV = { 10, 20, 30, 40, 50, 60, 70, 80 };
byte[] inputByteArray = new byte[stringToDecrypt.Length];
try
{
key = Encoding.UTF8.GetBytes(sEncryptionKey.Substring(0, 8));
DESCryptoServiceProvider des = new DESCryptoServiceProvider();
inputByteArray = Convert.FromBase64String(stringToDecrypt);
MemoryStream ms = new MemoryStream();
CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(key, IV), CryptoStreamMode.Write);
cs.Write(inputByteArray, 0, inputByteArray.Length);
cs.FlushFinalBlock();
Encoding encoding = Encoding.UTF8;
return encoding.GetString(ms.ToArray());
}
catch (System.Exception ex)
{
return (string.Empty);
}
}
#endregion


}

----------------------grid inline-----------
protected string Encode(object objGrid)
{
return Server.UrlEncode(EncryptionHelper.EncryptText(Convert.ToString(objGrid.ToString())));
}
-----------------------------
if (Request.QueryString["Status"] != null && Request.QueryString["EditID"] != null)
{
hdnDecodeWOID.Value = Convert.ToString(Server.UrlDecode(EncryptionHelper.DecryptText(Request.QueryString["EditID"].ToString())));
hdneDecodeStatus.Value = Convert.ToString(Server.UrlDecode(EncryptionHelper.DecryptText(Request.QueryString["Status"].ToString())));
}
-------example--------------------------------------------------
< a href="/Pages/AccountWorkOrderDisplay.aspx?EditID=< %#(Encode(Eval("ID")))%>&Status=<%#(Encode(Eval("Status")))%>&Mailstatus=<%#(Encode(Eval("MailImageStatus")))%>">

The security validation for this page is invalid. Click Back in your Web browser.

Sometimes while trying to delete documents or move documents from one library to another we get an error "The security validation for this page is invalid. Click Back in your Web browser, refresh the page, and try your operation again” while trying to move documents from one document library to another document library."

This error is caused due to security validation. So there should be a way out to turn it off. One method to turn off security is:
Central Administration—>application management—->web application general settings–>turn security validation off

But, if we always keep it on there might be danger of malicious code.

So, we should handle this through coding, turn off security validation for our code to execute and then again turn it on.

SPWeb ospWeb = SPContext.Current.Web;
Microsoft.SharePoint.Administration.SPWebApplication webApp = ospWeb.Site.WebApplication;
webApp.FormDigestSettings.Enabled = false;
// our code should be inserted here
webApp.FormDigestSettings.Enabled = true;

Monday, December 20, 2010

SharePoint List Programmatically

Here's the code:

// Instantiate web instances
SPSite sourceSite = new SPSite(@"http://SharePointServer:31001");
SPWeb sourceWeb = sourceSite.RootWeb;
SPSite destSite = new SPSite(@"http://SharePointServer:31002");
SPWeb destWeb = destSite.RootWeb;

// Get a reference to the source list
SPList sourceList = sourceWeb.GetList("/Lists/Announcements");

// if the list exists on the destination site, delete it
try
{
SPList temp = destWeb.Lists[sourceList.Title];
destWeb.Lists.Delete(temp.ID);
}
catch { }

// create new list on the destination web with same properties
Guid newListID = destWeb.Lists.Add(sourceList.Title, sourceList.Description,
sourceList.BaseTemplate);
SPList destList = destWeb.Lists[newListID];

// copy items
foreach (SPListItem item in announcements.Items)
{
SPListItem newDestItem = destList.Items.Add();
foreach (SPField field in sourceList.Fields)
{
if (!field.ReadOnlyField)
newDestItem[field.Id] = item[field.Id];
}
newDestItem.Update();
}

// set quicklaunch settings
destList.OnQuickLaunch = sourceList.OnQuickLaunch;
destList.Update();

IQueryable vs IEnumerable

The primary difference is that the extension methods defined for IQueryable take Expression objects instead of Functional objects, meaning the delegate it receives is an expression tree instead of a method to invoke.

IEnumerable < T > is great for working with in-memory collections, but IQueryable< T > allows for a remote data source, like a database or web service.

IEnumerable doesn’t have the concept of moving between items, it is a forward only collection. It’s very minimalistic; something that most any data source can provide. Using only this minimal functionality, LINQ can provide all of these great operators.

IQueryable<T> is a very powerful feature that enables a variety of interesting deferred execution scenarios (like paging and composition based queries).


IQueryable < Customer > custs = from c in db.Customers
where c.City == "< City >"
select c;

IEnumerable < Customer> custs = from c in db.Customers
where c.City == "< City >"
select c;