Script is not working in WebView in UWP - c#

I am using following web view in xaml.
<WebView x:Name="FBCommentsTest"></WebView>
And i am trying to embed HTML data to that web view in the following way in the c# end.
FBCommentsTest.NavigateToString("<html><head></head><body><div id=\"fb-root\"></div><div id=\"fb-root\"></div><script>(function(d, s, id) {var js, fjs = d.getElementsByTagName(s)[0];if (d.getElementById(id)) return;js = d.createElement(s); js.id = id;js.src = \"http://connect.facebook.net/en_US/all.js#xfbml=1&appId=" + APP_KEY + "\";fjs.parentNode.insertBefore(js, fjs);}(document, 'script', 'facebook-jssdk'));</script><div class=\"fb-comments\" data-href=\""
+ mExternalUrl + "\" data-width=\"500\"></div> </body></html>");
But, in the web view i am getting an empty screen not able to get the facebook comments view.
How to enable the script in web view in uwp.
Please help me.
Thanks.

But, in the web view i am getting an empty screen not able to get the facebook comments view. How to enable the script in web view in uwp.
The NavigateToStringmethod does not provide a way to generate or provide css, scripts, images, and fonts resources programmatically. You can refer to WebView.NavigateToString method.
You can transfer your string into HTML file, and use Navigate method to navigate to the HTML page. Just for example, create a HTMLPage file in the project and code for it, then in the c# code behind:
Uri uri = new Uri("ms-appx-web:///HTMLPage1.html");
FBCommentsTest.Navigate(uri);

Related

Xamarin iOS load content in UIWebView

I'm trying to make a custom web page and put it in a webview in Xamarin using C#
What I have is a UIViewController containing a UIWebView.
In my project I have a folder like this:
Project
Content
index.html
js
css
But when I try to load the index.html file into the webview nothing happens.
I tried just loading an external webpage and it works fine.
this is my code for loading the content in the ViewDidLoad method of the UIViewController:
string fileName = "Content/index.html";
string localHtmlUrl = Path.Combine(NSBundle.MainBundle.BundlePath, fileName);
MyWebView.LoadRequest(new NSUrlRequest(new NSUrl(localHtmlUrl, false)));
MyWebView.ScalesPageToFit = true;
I think it has something to do with the page not finishing it's loading, but I can't figure out how to do this properly.
Derp, always make sure to set the 'Build Action' on your file to BundleResource on each file to include them in the build by right clicking on them...

c# add page to onenote from template

New to OneNote development. I have an app that I want to add a new page based on the template that is the default in this section of my notebook.
I want to take some text from a textbox and name the generated new page to that text. I don't need to do anything else with the page from the app Any ideas would be much appreciated.
You can use the Onenote API to programmatically recall the template page's content as html See http://dev.onenote.com/docs#/reference/get-pages/v10menotespagesidcontentincludeids on how to do this.
Once you have the template's html, you can make the title tag with the title you get from your web form . eg: my title and make a POST request to the section. See http://dev.onenote.com/docs#/reference/post-pages/v10menotessectionsidpages
That should take care of it.

Issue with BaseUrl in WebView

So i'm having issues with stylesheets and images using a WebView.
Im using Xamarin.Forms (latest version)
Here is what i do:
I POST to an URL and download the response html
I add this to HtmlWebViewSource(), which i then add to a WebView
I then set my BaseUrl to the domain
Here is some code:
string raw_html = getHtml();
WebView browser = new WebView();
HtmlWebViewSource html = new HtmlWebViewSource {
Html = raw_html
};
html.BaseUrl = getBaseUrl(); // i have tried with many different urls here (to make sure the path is not the problem) however none seems to work
browser.Source = html;
What is not working?
The problem is that the webpage is loaded, however none of the stylesheets images etc. is loaded. So im stuck with HTML with no style or images. As the relative path is not working.
An example:
/images/image.png // in HTML
The browser dont know where this is pointing as the BaseUrl is not set, however when i set the BaseUrl it is still not working.

URL relative to server

I have an ASP.NET button in my application, which I want to open another website on my server in a new window. Currently I do a Response.Redirect in my server side on click event (to "..\OtherWebsite") which works. But as it needs to be in a new window I need to do it in JavaScript.
Would '..\OtherWebsite' path work in the window.open JavaScript command? If not, what .NET methods/properties can I use to get the full path?
I can't hard code the website URL as some users will be accessing through a LAN (server name\application) and some through the website (www.website.com/application)
You can use self.location.href to get the currect url of the page, next form your new url relative to that and then pass it to window.open() method.
Here is the definition of window.open method.
Hope this helps...
You can do this by executing the javascript from the codebehind.
public void OpenNewWindow(string url)
{
ClientScript.RegisterStartupScript(this.GetType(), "newWindow", String.Format("<script>window.open('{0}');</script>", url));
}
http://forums.asp.net/t/1001747.aspx/1
First result from searching for "open new window from codebehind c# .net"
Then just pass the relative url like so.
String url = "~/page.aspx";
url = Page.ResolveClientUrl(url);
OpenNewWindow(url);
also a reference here: Response.Redirect to new window
Hope this helps.

Modifying rich content on home page programmatically

I am provisioning site collections programmatically in SP2010. The process is working fine but I need to be able to customize some text on the home page that is contained in rich text (not in a web part). Is there a way to grab that HTML code and modify it from code? For instance, embedded within the home page, I have a title and a paragraph of text. I'd like to customize the title based on one of the input values provided from the provisioning request. Is it possible to do this? Any suggestions would be appreciated!
You can access the content area as follows:
PublishingWeb pw = null;
//"web" is your SPWeb, didn't include using statement for your SPSite/SPWeb in this sample
if (PublishingWeb.IsPublishingWeb(web))
{
pw = PublishingWeb.GetPublishingWeb(web);
}
//todo: add some handling here for if web is not a publishingWeb
//assuming your home page is the default, if not - get the correct page here
SPFile defaultpage = pw.DefaultPage;
if (!(defaultpage.CheckOutType == SPFile.SPCheckOutType.None))
{
//programatically creating this stuff, so cancel any unexpected checkouts
defaultpage.UndoCheckOut();
}
defaultpage.CheckOut();
//Field you want to add HTML in (Alternatively, retreive existing data and modify the HTML)
defaultpage.ListItemAllFields["PublishingPageContent"] = "string of HTML";
defaultpage.ListItemAllFields.Update();
defaultpage.CheckIn("My Comment");

Categories