Tell me more ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I am using SharePoint Foundation 2010

I am designing a bulk upload of files into a Document Library. The files will all have a Content Type applied to them with the values coming from a number of External Lists which get the data from a remote SQL Server instance.

Uploading a file directly works fine - when I go to edit the item to set the Content Type, all of the lookups work perfectly and I can set the fileds correctly.

In my bulk loader (using SSIS) I can successfully load the file into the library and I can set the string value of each field in the Content Type and can see these values when I look at the properties of the file.

However, when I edit it, the fields are all blank - and my investigation has shown that there are related fields in the Content Type for what looks like the BDCIdentity values corresponding to the lookup value in each External List.

I can't seem to find a way to get these values programmatically - the code I am using appears to return 0 items from the External List. When I use the code to try to extract the value I seem to get a permissions error, and when I return the ItemCount it is 0.

I'm sure there is a simple step that I have missed somewhere - any pointers anyone?

    [Microsoft.SqlServer.Dts.Pipeline.SSISScriptComponentEntryPointAttribute]

    public class ScriptMain : UserComponent
    {
    private string _sharePointSite;
    private string _docLibrary;
    private string _imgPath;
    private string _itemTitle;
    private string _itemItem_or_Range;
    private string _itemRange;
    private string _itemSeason;
    private string _itemProductType;

private static string GetBdcCode(SPList list, string lookupValue, string lookupColumn)
{
    string myCode = null;
    //SPListItem item = list.Items[0];    // set this to check for the ItemCount
    foreach (SPListItem item in list.Items)   // throws a permissions error - not sure if that is because there are 0 items???
    {
        if (item[lookupColumn].ToString() == lookupValue)
        {
            myCode = item["BdcIdentity"].ToString();
        }
    }
    //        myCode = list.Title;     // Title always returns correctly
    //myCode = list.ItemCount.ToString();    // as does ItemCount
    return myCode;
}

private void AddFileToDocumentLibrary(string documentLibraryUrl, string documentLibrary, string filename, byte[] file_bytes, string imageTitle, string imageItemRange, string imageRange, string imageProductType, string imageSeason)
{
    using (SPSite site = new SPSite(documentLibraryUrl))
    {
        SPServiceContext context = SPServiceContext.GetContext(site);
        SPServiceContextScope contextScope = new SPServiceContextScope(context);

        using (SPWeb web = site.OpenWeb())
        {
            // Get references for impersonation
            Guid siteID = site.ID;
            Guid webID = web.ID;
            string ProductTypeID;
            string ProductSeasonID;
            string ProductRangeID;
            string ProductItemOrRangeID;

            // Allow updates from code
            web.AllowUnsafeUpdates = true;

            // Get the Images library and it's collection of files and add a new file
            SPDocumentLibrary productCatalog = (SPDocumentLibrary)web.Lists[documentLibrary];
            SPFileCollection files = productCatalog.RootFolder.Files;
            SPFile newFile = files.Add(productCatalog.RootFolder.Url + "/" + filename, file_bytes, true);

            // Get the Images library as a list
            SPList documentLibraryAsList = web.Lists[documentLibrary];

            // Get reference to the new file and the correct Content Type
            SPListItem itemJustAdded = documentLibraryAsList.GetItemById(newFile.ListItemAllFields.ID);
            SPContentType documentContentType = documentLibraryAsList.ContentTypes["Product Image"]; 


            itemJustAdded["ContentTypeId"] = documentContentType.Id;
            itemJustAdded["Title"] = imageTitle;
            itemJustAdded["Image Type"] = "Artwork";
            itemJustAdded["Image Region"] = ";#AU;#NZ;#EU;#NA;#";
            itemJustAdded["Image Item or Range"] = imageItemRange;
            itemJustAdded["Image Range"] = imageRange;
            itemJustAdded["Image Product Type"] = imageProductType;
            itemJustAdded["Image Season"] = imageSeason;

            //itemJustAdded["Image Item or Range"] = GetBdcCode(web.Lists["Product Item or Range"], imageItemRange, "itemrange_ItemRangeID");
            //itemJustAdded["Image Range"] = GetBdcCode(web.Lists["Product Ranges"], imageRange, "range_ProductRange");
            //itemJustAdded["Image Product Type"] = GetBdcCode(web.Lists["Product Type"], imageProductType, "product_ProductType");
            //itemJustAdded["Image Season"] = "__bk0100030053008500d400";

    SPSecurity.RunWithElevatedPrivileges(delegate()
    {
    using (SPSite siteImp = new SPSite(siteID))
    {
    using (SPWeb webImp = siteImp.OpenWeb())
    {
        // Get Season Lookup value
        SPList bdcLookup = webImp.GetList("Lists/Seasons Lookup");
        ProductSeasonID = GetBdcCode(bdcLookup, imageSeason, "SeasonID");
        itemJustAdded["Product Season_ID"] = ProductSeasonID;

    }
}
    });

            itemJustAdded.Update();
            newFile.CheckIn("New Image added by SSIS on : " + DateTime.Now.ToString() , SPCheckinType.MajorCheckIn);
            web.AllowUnsafeUpdates = false;

        }
    }
}

public override void PreExecute()
{
    base.PreExecute();

    _sharePointSite = this.ReadOnlyVariables["User::SharePointSite"].Value.ToString();
    _docLibrary = this.ReadOnlyVariables["User::DocumentLibrary"].Value.ToString();

}
share|improve this question

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

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

Browse other questions tagged or ask your own question.