code for Retrieve image from a picture library working in visual web part, but in infopath - InfoPath Dev
in

InfoPath Dev

code for Retrieve image from a picture library working in visual web part, but in infopath

Last post 01-01-2011 08:19 AM by Potemkine. 1 replies.
Page 1 of 1 (2 items)
Sort Posts: Previous Next
  • 01-01-2011 12:59 AM

    code for Retrieve image from a picture library working in visual web part, but in infopath

    Hi,

    Wishing all a very Happy and Prosperous New Year.

    I am new to creating code in Infopath. The requirement is to search for a image in the picture library and show that image. This scenario is needed in online as well as offline.

    So, i chose Infopath filler 2010. I am able to retrieve the image and show in a visual web part created using VS 2010. But I am not sure of doing it in infopath.

    Following are the controls i have in the Infopath filler form. I have a textbox called "field12", and a button called search. Below that i have added a table. In the first td, I added a rich textbox to show the image. The id of the richtextbox field is "Image1". The picture library in the sharepoint site is called "MyPictureLibrary"

    This is the code i have used in Visual web part, while creating in VS 2010, which works fine.

    [CODE]

    protected void BtnSearch_Click(object sender, EventArgs e)
    {
    SPSecurity.RunWithElevatedPrivileges(
     delegate()
     {
    string FileImageNumber = FetchImagefromPiclib();
     }
     );
     }
     

    public string FetchImagefromPiclib()
            {
    string fileImageNumber = string.Empty;
     using (SPSite site = new SPSite(SPContext.Current.Site.ID, SPContext.Current.Site.Zone))
     {
    using (SPWeb currentweb = site.OpenWeb(SPContext.Current.Web.ID))
    {
     currentweb.AllowUnsafeUpdates = true;
     SPList list = currentweb.Lists["MyPictureLibrary"];
     SPQuery oPostQuery = new SPQuery();
     oPostQuery.RowLimit = 1;

     StringBuilder query = new StringBuilder();
     query.Append("<View>")
     .Append("<Query>")
     .Append("  <Where>")
     .Append("  </Where>")
     .Append("</Query>")
     .Append("<RowLimit>" + 1)
     .Append("</RowLimit>")
     .Append("</View>");
      oPostQuery.Query = query.ToString();
     SPListItemCollection collListItems = list.GetItems(oPostQuery);
     list.Update();
     SPFolder folder = list.RootFolder;
     if (collListItems.Count != 0)
     {
     foreach (SPListItem oListItem in collListItems)
     {
     string imageUrl = SPContext.Current.Site.Url + "/" + list + "/" + oListItem.File.Name;
     string thumbUrl = imageUrl.Substring(0, imageUrl.LastIndexOf('/'));
     thumbUrl = thumbUrl + "/_t/" + oListItem.File.Name.Replace('.', '_') + ".jpg";
     Image1.ImageUrl = thumbUrl;
     Image1.ToolTip = (oListItem["Title"] != null) ? oListItem["Title"].ToString() : "No Title";

    // sanatize data from SharePoint
    fileImageNumber = (oListItem["Title"] != null) ? oListItem["Title"].ToString() : "No Title";
    int itemId = oListItem.ID;
      }
    }
     }
     }
      return fileImageNumber;
      }

    [/CODE]

    When i try to debug, it breaks at spsecurity, with error as Invalid COM exception. In the central admin as well as in the site, I have set User Profile to true.

    How to use the above code in Infopath?

    Thank you

  • 01-01-2011 08:19 AM In reply to

    Re: code for Retrieve image from a picture library working in visual web part, but in infopath

    Hi, if you plan to use infopath filler, you can use directly sharepoint dll because your client doesn't have this dll on their desktop. You can use web service to "speak" to SHarepoint I already reply on your other post about that.
Page 1 of 1 (2 items)
Copyright © 2003-2019 Qdabra Software. All rights reserved.
View our Terms of Use.