First, I'm checking a GridView checkbox for Job Listings selected and loading them into an array.
bool atLeastOneRowSelected = false;
var IDs = new List<int>();
foreach (GridViewRow row in gvPositions.Rows)
{
// Access the CheckBox
CheckBox cb = (CheckBox)row.FindControl("cbJob");
if (cb != null && cb.Checked)
{
// Delete row! (Well, not really...)
atLeastOneRowSelected = true;
// First, get the ProductID for the selected row
IDs.Add(Convert.ToInt32(gvPositions.DataKeys[row.RowIndex].Value));
}
}
Next, I create the SharePoint list items and save the internal ID into another array, which I will reference next to add the resume attachment.
List<int> intIDs = new List<int>();
SPList list = site.Lists["Candidates"];
SPListItem listItem = list.AddItem();
listItem.Update();
intIDs.Add(Convert.ToInt32(listItem["ID"].ToString()));
Once I've done that, I call the method to add the resume attachment to the above list items.
AddNewAttachment(intIDs);
private void AddNewAttachment(List<int> NodeIDs)
{
string destUrl = SPContext.Current.Site.Url;
SPSite rootSite = new SPSite(destUrl);
SPWeb site = new SPSite(destUrl).OpenWeb();
site.AllowUnsafeUpdates = true;
SPList myList = site.Lists["Candidates"];
if (fileMUpload.PostedFile != null && fileMUpload.HasFile)
{
Stream fStream = fileMUpload.PostedFile.InputStream;
byte[] contents = new byte[fStream.Length];
fStream.Read(contents, 0, (int)fStream.Length);
fStream.Close();
fStream.Dispose();
string fileName = Path.GetFileName(fileMUpload.PostedFile.FileName);
foreach (int NodeID in NodeIDs)
{
SPListItem myNewItem = myList.GetItemById(NodeID);
SPAttachmentCollection attachments = myNewItem.Attachments;
attachments.Add(fileName, contents);
myNewItem.SystemUpdate();
myNewItem = myList.GetItemById(NodeID);
}
}
}