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

I want to connect my picture library named "News" with my SharePoint list "Staff News" so that the picture i upload is connected to the specified news. I've also created a lookup column but i only know how to make the connection if i create the listitem and picture for itself on the page. I'm working with a webpart.

My Save button code is:

    protected void ButtonSave_Click(object sender, EventArgs e)
    {
        SPSite site = new SPSite("http://knowitintranet:9797/sites/News");
        SPWeb web = SPContext.Current.Web;
        SPList list = web.Lists.TryGetList(DropDownListCategory.SelectedItem.Value);
        //SPPictureLibrary picLib = (SPPictureLibrary)web.Lists.TryGetList(DropDownListCategory.SelectedItem.Value);

        try
        {
            SPListItem item = list.Items.Add();
            DateTime dt = DateTime.Now;

            item["Title"] = TextBoxTitle.Text;
            item["Contents"] = TextBoxContents.Text;
            item["Category"] = DropDownListCategory.SelectedItem.Value;
            item["Authors"] = TextBoxAuthor.Text;
            item["Date"] = dt.Date;

            SPFieldUrlValue value = new SPFieldUrlValue();
            value.Description = TextBoxLinkDesc.Text.Trim();
            value.Url = TextBoxAddLink.Text.Trim();
            item["Links"] = value;



            byte[] imageData;
            if (FileUploadImage != null)
            {
                if ((FileUploadImage.PostedFile != null) && (FileUploadImage.PostedFile.ContentLength > 0))
                {
                    Stream MyStream = FileUploadImage.PostedFile.InputStream;
                    long iLength = MyStream.Length;
                    imageData = new byte[(int)MyStream.Length];
                    MyStream.Read(imageData, 0, (int)MyStream.Length);
                    MyStream.Close();
                    string filename = System.IO.Path.GetFileName(FileUploadImage.PostedFile.FileName);
                    SPPictureLibrary pic = (SPPictureLibrary)SPContext.Current.Web.Lists["News"];//Images is the picture library name
                    SPFileCollection filecol = ((SPPictureLibrary)SPContext.Current.Web.Lists["News"]).RootFolder.Files;//getting all the files which is in pictire library
                    filecol.Add(filename, imageData);//uploading the files to picturte library

                }
            }         

            item.Update();

            TextBoxAddLink.Text = string.Empty;
            TextBoxLinkDesc.Text = string.Empty;
            TextBoxAuthor.Text = string.Empty;
            TextBoxContents.Text = string.Empty;
            TextBoxTitle.Text = string.Empty;

            LabelException.Text = "News added!";

            site.Dispose();
        }
        catch (Exception)
        {
            LabelException.Text = "";
        }
    }

This code saves to my list and picture library but the connection isn't there. But then i got a second problem to. I want to lookup in three lists, not only one.. Since i've working with "Staff News", "Management News", and "Company News".

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.