{"id":1115,"date":"2005-11-21T23:31:00","date_gmt":"2005-11-22T04:31:00","guid":{"rendered":"http:\/\/blogs.devhorizon.com\/reza\/?p=1115"},"modified":"2005-11-21T23:31:00","modified_gmt":"2005-11-22T04:31:00","slug":"retrieving-random-images-from-picture-library-in-sharepoint","status":"publish","type":"post","link":"https:\/\/blogs.devhorizon.com\/reza\/2005\/11\/21\/retrieving-random-images-from-picture-library-in-sharepoint\/","title":{"rendered":"Retrieving random images from picture library in sharepoint"},"content":{"rendered":"<p><P class=MsoNormal style=\"MARGIN: 0cm 0cm 0pt\">If you are planning to create a web part that can retrieve and display a thumbnail of a random image from the picture library, then you have two options:<?xml:namespace prefix = o ns = \"urn:schemas-microsoft-com:office:office\" \/><o:p><\/o:p><\/P><br \/>\n<P class=MsoNormal style=\"MARGIN: 0cm 0cm 0pt\">1) The images are stored as binary fields in the DOCS table inside the _SITE database. The field name is CONTENT. Simply connect to the database to retrieve the image. This is not the way I usually get stuff done in SPS.<\/P><br \/>\n<P class=MsoNormal style=\"MARGIN: 0cm 0cm 0pt\">2) I would suggest you access the images via the Sharepoint namespaces. Picture libraries store their image files in the same manner as document libraries store documents, so get a reference to the picture library as an SPList class and retrieve the image from the SPListItem.File property.<\/P><br \/>\n<P class=MsoNormal style=\"MARGIN: 0cm 0cm 0pt\">&nbsp;<\/P><br \/>\n<P class=MsoNormal style=\"MARGIN: 0cm 0cm 0pt\">protected override void RenderWebPart(HtmlTextWriter output)<BR>{<BR>&nbsp;&nbsp; string url;<BR>&nbsp;&nbsp; string caption;<\/P><br \/>\n<P class=MsoNormal style=\"MARGIN: 0cm 0cm 0pt\">&nbsp;&nbsp; string HTMLtext;<BR>&nbsp;&nbsp;&nbsp; try<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SPWeb mysite=SPControl.GetContextWeb(Context);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SPListCollection lists=mysite.Lists;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; foreach (SPList list in lists)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (list.Title.Equals(PicLib.ToString()))<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; SPListItemCollection items=list.Items;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int rndCount=items.Count;<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (rndCount &gt; 0)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; int intRandom=rIndex.Next(1,rndCount);<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bsp;&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; output.Write(&#8220;There is no picture in the selected picture library.&#8221;);<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>catch(Exception e)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; output.Write(SPEncode.HtmlEncode(e.Message.ToString()));<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; }<BR>}<BR><\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you are planning to create a web part that can retrieve and display a thumbnail of a random image from the picture library, then you have two options: 1) The images are stored as binary fields in the DOCS table inside the _SITE database. The field name is CONTENT. Simply connect to the database [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-1115","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/blogs.devhorizon.com\/reza\/wp-json\/wp\/v2\/posts\/1115","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.devhorizon.com\/reza\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.devhorizon.com\/reza\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.devhorizon.com\/reza\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.devhorizon.com\/reza\/wp-json\/wp\/v2\/comments?post=1115"}],"version-history":[{"count":0,"href":"https:\/\/blogs.devhorizon.com\/reza\/wp-json\/wp\/v2\/posts\/1115\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.devhorizon.com\/reza\/wp-json\/wp\/v2\/media?parent=1115"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.devhorizon.com\/reza\/wp-json\/wp\/v2\/categories?post=1115"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.devhorizon.com\/reza\/wp-json\/wp\/v2\/tags?post=1115"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}