Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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

  1. 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?

  2. 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();
    }
}
}
share|improve this question
    
I normally do, but slipped up this time. Appreciate the reminder. –  Mike Collins Sep 25 '12 at 22:16

1 Answer 1

up vote 0 down vote accepted

http://social.technet.microsoft.com/Forums/en-US/sharepoint2010programming/thread/db28b6c7-dd22-424f-bb7d-4480db803e8b

check this Link

share|improve this answer
    
While this may theoretically answer the question, we prefer inclusion of the essential parts of the answer here, and to provide the link for reference. See answer for general guidelines. –  SPDoctor Sep 26 '12 at 6:49

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.