I am trying to develop a web part that will display a random photo from my photo library.
I have been able to successfully loop through and select a random photo from the library. My issue now is to display this photo on the SharePoint site. I have tried creating an image control and setting the url to the url I retrieve and programmatically add an image control but the image does not seem to display on the page after the web part loads. It simply shows a red X saying the image could not be displayed.
If I navigate to the URL of the photo in the browser it is diplayed but not when the web part tries to pull it down.
Can anybody give me a pointer? I'm still a SharePoint beginner
Here is what I have so far:
Random randomNumber;
randomNumber = new Random();
Controls.Clear();
ClearChildState();
SPWeb thisWeb = SPContext.Current.Web;
SPPictureLibrary pictures = (SPPictureLibrary)thisWeb.Lists["Houston Photos"];
int pictureCount = pictures.ItemCount;
int index = randomNumber.Next(pictureCount);
string source = thisWeb.Url + "/" + pictures.Items[index].Url;
String itemHtml = String.Format(#"<img SRC={0}+height=200px> </img>",source);
this.Controls.Add(new LiteralControl(itemHtml));
imgControl.ImageUrl = source;
I'd say it's your img tag that's the problem
try something like
string itemHtml = string.concat("<img src=\"", source, "\" height=\"200\" />);
You could also use the Image class control, http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.image.aspx
Random randomNumber;
randomNumber = new Random();
Controls.Clear();
ClearChildState();
SPWeb thisWeb = SPContext.Current.Web;
SPPictureLibrary pictures = (SPPictureLibrary)thisWeb.Lists["Houston Photos"];
int pictureCount = pictures.ItemCount;
int index = randomNumber.Next(pictureCount);
string source = thisWeb.Url + "/" + pictures.Items[index].Url;
var image = new Image();
image.ImageUrl = source;
image.Height = 200;
this.Controls.Add(image);
Related
Goal: Using an iOS native method, push a user made picture onto their Instagram feed in C#.
public bool ShareImage(byte[] imageByte)
{
bool result = false;
//string fileName = "xxx.png";
//string path = Path.Combine(FileSystem.CacheDirectory, fileName);
//File.WriteAllBytes(path, imageByte);
NSData data = NSData.FromArray(imageByte);
UIImage image = UIImage.LoadFromData(data);
//NSUrl url = NSUrl.FromString($"instagram://library?AssetPath={path}"); // Only opens
NSUrl url = NSUrl.FromString($"instagram://library?LocalIdentifier={1}");
if (UIApplication.SharedApplication.CanOpenUrl(url))
{
UIApplication.SharedApplication.OpenUrl(url);
}
return result;
}
As far as I'm aware the way I have to do this is to save my image to the device and get a Local Identifier for this.
I have constructed this from the snippets of objective-c code I have seen. Sharing photos only works with the native app installed, as I have learnt from my trials to get the facebook module working.
Edit: Using PHAssetChangeRequest from the iOS Photos namespace does not work.
A collegue has pointed out to me about the possibility of saving then using a photo picker to get the PHAsset for the Local Identifier. But this is an extra step I do not want the users of the App to go through. Better to just remove Instagram support as I just can go through the generic share method as shown below. The disadvantage of this method is that the user has then to pick the medium to share over.
public async void ShareImage(byte[] imageByte)
{
string fileName = "xxx.png";
string path = Path.Combine(FileSystem.CacheDirectory, fileName);
File.WriteAllBytes(path, imageByte);
await Share.RequestAsync(
new ShareFileRequest()
{
File = new ShareFile(path),
Title = "xxx"
}
);
}
Edit 2
Tried a different way using UIDocumentInteractionController but it is showing nothing and not posting, it is not throwing any exceptions to give me any clues as to what I'm doing wrong.
public bool ShareImage(byte[] imageByte)
{
bool result = false;
string fileName = "xxx.igo";
string path = Path.Combine(FileSystem.CacheDirectory, fileName);
File.WriteAllBytes(path, imageByte);
//string caption = "xxx";
string uti = "com.instagram.exclusivegram";
UIImageView view = new UIImageView();
//UIDocumentInteractionController controller = UIDocumentInteractionController.FromUrl(NSUrl.FromString(path));
UIDocumentInteractionController controller = new UIDocumentInteractionController
{
//controller.Url = NSUrl.FromString(path);
Url = NSUrl.FromFilename(path),
Uti = uti
};
//CoreGraphics.CGRect viewDimensions = new CoreGraphics.CGRect(0, 0, 200, 100);
_ = controller.PresentOpenInMenu(CoreGraphics.CGRect.Empty, view, true);
//_ = controller.PresentOpenInMenu(viewDimensions, view, true);
return result;
}
Edit 3
Using
UIView view = new UIImageView();
if (UIApplication.SharedApplication.KeyWindow.RootViewController is UIViewController uiController && uiController.View != null)
{
view = uiController.View;
}
I was able to get the possible share options to show. I was logged in to Instagram using the native App, but the post did not show.
Change
Url = NSUrl.FromFilename(path)
to
Url = new NSUrl(path,false)
The second way is pointing to the correct file path not the file name .
Change
controller.PresentOpenInMenu(CoreGraphics.CGRect.Empty, view, true);
to
controller.PresentOptionsMenu(UIScreen.MainScreen.Bounds, (UIApplication.SharedApplication.Delegate as AppDelegate).Window, true);
Show UIDocumentInteractionController inside current Window and set the proper frame.
I am making a web application.
In main page I want to show some items in Sharepoint List.
So I saved content data(Included text and image source url) and tried to show it.
But I don`t know how to catch image source url.
My Code is Like this.
string itemBody = string.Empty;
//listItemCol is SPListItemCollection
itemBody = listItemCol[i]["Body"] == null ? "" : listItemCol[i]["Body"].ToString();
I checked itemBody, and it contained this data.
<div class="ExternalClass0E1F6FC2B36A47AEB474512CAE7D8B6E">
<p><img alt="group-news__thumb.jpg" src="/SiteAssets/Lists/Announcement/AllItems/group-news__thumb.jpg" style="margin:5px;" /> </p>
<p>News TEST</p>
<p> </p>
</div>
How can I get that image source using c#? Please help me
You can try this.
int stratIndex = itemBody.IndexOf("src=\"");
int endIndex = itemBody.IndexOf("\"", stratIndex + 5);
string result = itemBody.Substring(stratIndex + 5, endIndex - stratIndex - 5);
i'am trying to generate a chart like this one: http://chartpart.com/ but i don't know how to do that exactly.Do i need to make the string that generates the chart manually or do i need to send the data to a google server and receive the string?(if so how do i send the data?)
PS:i would like to do all this in c# and send the resulting string via web service.
apparently you need to generate the string yourself.I did just that and send the result to android via web service.
[WebMethod]
public ChartsClass GetChart(string UserId)
{
ChartsClass chart = new ChartsClass();
DataSet1TableAdapters.WS_ChartIndicatorsTableAdapter adapter = new DataSet1TableAdapters.WS_ChartIndicatorsTableAdapter();
DataSet1.WS_ChartIndicatorsDataTable table = adapter.GetChartIndicators(UserId);
StringBuilder sbDescriptions = new StringBuilder();
StringBuilder sbValues = new StringBuilder();
for(int i= 0 ;i< table.Rows.Count ;i++)
{
DataSet1.WS_ChartIndicatorsRow chartRow = (table.Rows[i] as DataSet1.WS_ChartIndicatorsRow);
if (i > 0)
{
sbDescriptions.Append("|");
sbValues.Append(",");
}
sbDescriptions.Append(chartRow.SectorId + " - " + chartRow.SectorDescription );
sbValues.Append(chartRow.NetSaleValue.ToString());
}
chart.ChartString = String.Format("http://chart.apis.google.com/chart?cht=p3&chtt={0}&chd=t:{1}&chs=480x200&chl={2}&chco=ff0000,0000ff",
"Live Sales", sbValues.ToString(), sbDescriptions.ToString());
return chart;
}
And in android you can load the result in a web view.
chartView = (WebView)findViewById(R.id.activity_chart_view);
ChartsClass chart = new ChartsClass(soapObject);
chartView.loadUrl(chart.chartStringSource);
If you are trying to create a chart with user input, there's a wikiHow on that
wikiHow Link: http://www.wikihow.com/Make-a-Google-Chart-with-User-Input
The graph this wikiHow creates is the same as the one on this website:
Website Link: http://www.quickcompromise.com/index.html
Now I need to change the displaying image to image control. Help me?
var imlocation1 = "Bhopal/";
var currentdate1 = 0;
var image_number1 = 0;
function ImageArray1(n) {
this.length = n;
for (var i = 1; i <= n; i++) {
this[i] = ' '
}
}
image1 = new ImageArray1(3)
image1[0] = '2.jpg'
image1[1] = '4.jpg'
image1[2] = '1.jpg'
var rand1 = 100/ image1.length
function randomimage1() {
currentdate1 = new Date()
image_number1 = currentdate1.getSeconds()
image_number1 = Math.floor(image_number1 / rand1)
return (image1[image_number1])
}
document.write("<img src='" + imlocation1 + randomimage1() + "'>");
"Img" element have "src" attribute that points to the image on a server. So you need to find the "img" element, and if found set the src attribute.
Basic search for whole question http://www.bing.com/search?q=I+Load+an+Image+to+an+Image+Control+using+JavaScript&src=IE-SearchBox&FORM=IE8SRC give following link: http://www.javascriptexamples.org/2011/01/18/how-to-dynamically-load-an-image-using-javascript/
Sample from the link above:
<div id="imageContainer"></div>
var img = document.createElement("img");
img.onload = function(e) {
var container = document.getElementById("imageContainer");
container.appendChild(e.target);
}
img.setAttribute("src","images/puppy.jpg");
$("#YourImageId").attr("src", "ImagePath")
If you use webforms - probably
$("#<%=YourImageServerId.ClientID%>")
If it doesn't help - post some code or some more info to your question
The idea of a control only really exists on the server side. Javascript in this case only runs on the client side, on the browser. Once the server renders the image control, you end up with an IMG tag on the client side. If you tag it with an ID or a CSS class, you can find it with Javascript (or even better a library like JQuery) and load images onto it with that.
I am building a small facebook application using the Facebook developer toolkit 3.0 in c#. I had this piece of code that used to work a week ago. From some reason, when I try publishing my feed it appears without an image and without a Flash movie. Here is my code:
string userName = FacebookAPI.Users.GetInfo().name;
string message = "";
attachment attach = new attachment();
attach.href = string.Format(#"http://apps.facebook.com/{0}/?uId={1}&uName={2}", Consts.APP_NAME, ViewerUserId, userName);
attachment_media_flash flash = new attachment_media_flash();
flash.type = attachment_media_type.flash;
flash.expanded_width = 320;
flash.expanded_height = 260;
flash.height = 100;
flash.width = 130;
message = "";
attach.name = ""
attach.caption = "this works";
attach.description = ""
flash.imgsrc = string.Format(#"{0}/flash/Pinguin.JPG", Consts.DOMAIN_NAME);
flash.swfsrc = string.Format(#"{0}/flash/pinguin.swf", Consts.DOMAIN_NAME);
List<attachment_media> attach_media_list = new List<attachment_media>();
attach_media_list.Add(flash);
attach.media = attach_media_list;
/* action links */
List<action_link> actionlink = new List<action_link>();
action_link al1 = new action_link();
al1.href = string.Format(#"http://apps.facebook.com/{0}", Consts.APP_NAME);
al1.text = "myApp";
actionlink.Add(al1);
FacebookAPI.Stream.Publish(message, attach, actionlink, null , Convert.ToInt64 (ViewerUserId));
Make sure that:
1: The URL you pass is not the one that of the FB app, like http://apps.fb.co/whatever/image.jpg;
it should be your domain URL, like http://www.example.com/images/myimage.jpg.
2: You need to pass the full URL of the image including the protocol (http:// / https://), like this:
http://www.example.com/images/myimage.jpg
The following is wrong:
www.example.com/images/myimage.jpg