I have a web application that I've added to the _layouts section of a SharePoint application. Originally, I had the page uploading files to a document library. But now, I need to modify the code to upload files to a document set. I am confused on how to do that and the steps to do it. I have the code below which I'm trying to do it, but keep getting errors. First thing I'd like to know, is
Can I programmatically upload a document to a document set (like I'm attempting below) or do I need to create something (like a folder maybe) first?
Secondly, can some provide or point me to a good code example that I can use?
Below is my code, which I try to reference a document set, which I thought was the way to do it.
if (fileUpload.HasFile)
{
using (SPSite site = new SPSite(SPContext.Current.Web.Url))
{
using (SPWeb web = site.OpenWeb())
{
//SPList list = web.Lists["Awards"];
SPFolder ds = web.GetFolder(SPContext.Current.Web.Url + "/Awards/MyDocumentSetName/");
//Add the initial metadata. We will do an update for all lookup table values, as they need to be done after the record is created.
Hashtable ht = new Hashtable();
ht.Add("wfRecipientRank", ddRanks.SelectedValue);
ht.Add("wfRecipientName", txtRecipientName.Text);
ht.Add("APFT", rbPassedApft.SelectedValue);
ht.Add("HeightWeight", rbMeetHtWt.SelectedValue);
ht.Add("Posthumous", rbPosthumous.SelectedValue);
//MC 9/25/2012: The commented out line uploads to a document library, whereas the line just below it uploads
//to a document set.
//SPFile file = list.RootFolder.Files.Add(fileUpload.FileName, fileUpload.PostedFile.InputStream, ht, false);
SPFile file = ds.Files.Add(fileUpload.FileName, fileUpload.PostedFile.InputStream, ht, false);
SPListItem item = file.Item;
//Get the lookup field values
SPList lookupList = web.Lists["AwardType"];
int awardTypeId = GetItemId(ddAwardTypes.SelectedValue, lookupList);
SPFieldLookupValue awardTypeLookupValue = new SPFieldLookupValue();
if (awardTypeId > 0)
awardTypeLookupValue = new SPFieldLookupValue(awardTypeId, ddAwardTypes.SelectedValue);
lookupList = web.Lists["AwardReason"];
int awardReasonId = GetItemId(ddAwardReasons.SelectedValue, lookupList);
SPFieldLookupValue awardReasonLookupValue = new SPFieldLookupValue();
if (awardReasonId > 0)
awardReasonLookupValue = new SPFieldLookupValue(awardReasonId, ddAwardReasons.SelectedValue);
lookupList = web.Lists["Priority"];
int priorityId = GetItemId(ddPriorities.SelectedValue, lookupList);
SPFieldLookupValue priorityLookupValue = new SPFieldLookupValue();
if (priorityId > 0)
priorityLookupValue = new SPFieldLookupValue(priorityId, ddAwardReasons.SelectedValue);
lookupList = web.Lists["Organization"];
SPFieldLookupValue organizationLookupValue = new SPFieldLookupValue();
int organizationId = GetItemId(hiddenOrganizationId.Value, lookupList);
if (organizationId > 0)
organizationLookupValue = new SPFieldLookupValue(organizationId, txtOrganization.Value);
item["Organization"] = organizationLookupValue;
item["AwardType"] = awardTypeLookupValue;
item["AwardReason"] = awardReasonLookupValue;
item["Priority"] = priorityLookupValue;
item["PresentationDate"] = dpPresentationDate.Value;
item.Update();
}
}
}