Set Welcome Page - Office365 CSOM and C# - c#

I'm wanting to set the Welcome Page for my Sharepoint site using CSOM in C#.
Essentially what I'm doing is uploading my new welcome page (home.aspx) to either Site Pages or Pages and then I want to change the welcome page to match my newly uploaded page location.
I've looked around but a lot of what I've seen is either PowerShell or Server side code.
If someone can point me in the right direction I would very much appreciate it.

How to set Welcome Page via SharePoint CSOM
public static void SetWelcomePage(Web web,string pageUrl)
{
var ctx = web.Context;
var rootFolder = web.RootFolder;
rootFolder.WelcomePage = pageUrl;
rootFolder.Update();
ctx.ExecuteQuery();
}
Usage
using (var ctx = new ClientContext(webUri))
{
SetWelcomePage(ctx.Web, "SitePages/default.aspx");
}

Related

Xamarin: how to properly display a webpage reading its source code in an Android app

I am working on an Android application using Xamarin but hopefully the question below is more general and can be answered also by people not familiar with Xamarin.
The app is very simple and has one main page with a list where the use can click on any of the items.
These items represent products and all the related information are stored into a dictionary (key is the product name, values are some information about the product).
Amongst the available fields, I also added a URL of the website where the user can access more information about the product.
The app has a WebView to display the page inside the app.
I can display the webpage by using:
web = FindViewById<WebView>(Resource.Id.webView);
web.SetWebViewClient(new myWebViewClient());
web.Settings.JavaScriptEnabled = true;
web.LoadUrl(item.Link);
where the item/product the user has selected is the item object having the URL saved in item.Link.
The problem is that the user need a web connection in order to load the page. So I thought about saving the whole HTML code into a string so that the user can visualize the content even if no connection is available. I am doing this by:
WebClient wc = new WebClient();
using (Stream st = wc.OpenRead(web.Url))
{
using (StreamReader sr = new StreamReader(st, Encoding.UTF8))
{
html = sr.ReadToEnd();
}
}
Then each item in the dictionary will have a field item.Html with the source code of the page.
I can visualize the page with the following code:
web = FindViewById<WebView>(Resource.Id.webView);
web.SetWebViewClient(new myWebViewClient());
web.Settings.JavaScriptEnabled = true;
//web.LoadUrl(item.Link);
web.LoadData(item.Html, "text/html", "UTF-8");
The problem is that the viewer does not display the page in the same way as web.LoadUrl(item.Link) would do (i.e. the page as you would see in any web browser) but it is much messier and difficult to navigate.
What am I doing wrong? Any better way to achieve my goal?

How to fetch the data from website (particlar site with credentials)

I have a requirement where i need to login to multiple sites (http://www.XXXXXX.com) and give my credentials (Id and Pass).
After logging in, i have to click on particular link and need to select the date range and status level for all tasks in combo boxes. Then we will get the required information.
To avoid this all manual process, i would like to automate this in C# (Windows application).
Can anyone please help me how to achieve this?
Thanks in advance.
A possible solution could be using Selenium for this. With the Selenium webdriver you can easy fetch data from your web page.
With Selenium you could use FindElement(ById...
And use the GetText option to get your specific data.
You could also use HtmlAgilityPack. Authentification is possible with
using HtmlAgilityPack;
var htmlWeb = new HtmlWeb();
htmlWeb.PreRequestHandler += (request) =>
{
request.Credentials = new NetworkCredential("login", SecureString);
return true;
};
Check this post for more informations..

PhantomJs save webpage containing dynamic data as webpage

I have used jQuery plugin to generate charts on screen along with dynamic data and notes.
I need to generate PDF of this webpage on button click.
I have tried to search good option but not able to find any proper one.
Right now I am trying to use phantomjs for this. It works for internet sites but not working for intranet sites.
I am continuously getting Unable to load address.
Can anyone suggest me the way to achieve this? Or any alternative way to generate PDF from html content (My html contains SVG, Javascript generated dynamic data too).
Code:
string serverPath = "C:\\Phantomjs\\bin\\phantomjs";
var phantomJS = new PhantomJS();
var outFile = Path.Combine(serverPath, "google2.pdf");
if (File.Exists(outFile))
File.Delete(outFile);
try {
phantomJS.Run(Path.Combine(serverPath, "rasterize.js"),
new[] { "http://localhost:61362/HT.aspx?FNo=D1&PrNo=Dummy1&HtId=1033", outFile });
} finally {
phantomJS.Abort();
}
The url is not working in this case. But if I take any internet hosted site, it works.

SharePoint Foundation 2013 - Master Page 404 NOT FOUND

I hope this is right place to ask question. I tried use sosial msdn, but it seems that msdn forum is dead. I have small problem with Master Page. I have created Web Template and Master Page. When I create new site collection and I try access to it (http://win2008r2/sites/Test), it throws me error that: "404 NOT FOUND".
However, when I check MasterPage gallery: http://win2008r2/sites/Test/_catalogs/masterpage/Forms/AllItems.aspx
I can find my Demo.master . When I check Site Collection Features, I can see that my Master Page feature is active. I checked the logs and this was only thing what I found: Failure when fetching document. 0x80070002
I have set Web Template scope to farm and I have set Master Page scope to site.
I try to solve problem using google and I tried different guides but no I cannot get it work. Can anyone please help me?
Here is Element.xml:
<Module Name="MyMasterPage" List="116" Url="_catalogs/masterpage">
<File Path="MyMasterPage\Demo.master" Url="Demo.master" Type="GhostableInLibrary">
</File>
</Module>
Here is event receiver:
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
SPSecurity.RunWithElevatedPrivileges(delegate
{
using (SPWeb _web = ((SPSite)properties.Feature.Parent).RootWeb)
{
Uri _siteMaster = new Uri(string.Format("{0}/_catalogs/masterpage/Demo.master", _web.Url));
_web.MasterUrl = _siteMaster.AbsolutePath;
_web.CustomMasterUrl = _siteMaster.AbsolutePath;
_web.Update();
}
});
}
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
SPSite curSite = (SPSite)properties.Feature.Parent;
SPWeb curWeb = curSite.RootWeb;
Uri masterUri = new Uri(curWeb.Url + "/_catalogs/masterpage/v4.master");
curWeb.MasterUrl = masterUri.AbsolutePath;
curWeb.CustomMasterUrl = masterUri.AbsolutePath;
curWeb.Update();
}
If you want to see more of my code to solve this problem, please feel free to ask, so I put more code up. ;)
Thank you Yevgeniy.Chernobrivets! Problem was that I had named my default.aspx to MySite.aspx and it didn't work. When I created it again and I keep default.aspx name it worked! Well now I have to find way to change that default.aspx name =)

Modify the content of the home page programmatically in SharePoint 2010

First of all, I'm pretty new to SharePoint so don't shoot me if this is a noobish question or if I don't provide all the right information at once... :)
I have a team-site in SharePoint with following URL: "http://myServer/Opdracht_Ben/". By going to this URL I'm redirected to following page: "http://myServer/sites/Opdracht_Ben/SitePages/Home.aspx".
In Visual Studio I have a project for this site with a feature. When this site-feature is activated it should change the content of the home-page to some custom tekst and layout (i.e.: HTML). The content is not contained within a web part or the page is not a WikiPage, just text on a page.
I've been looking on MSDN and on several tech-sites and blogs but I have not found anything that could help me further. Does anyone know how I can 'reach' the content of the page and modify/update it?
PS:
Here on SO I have found a related question (Click for the question), but the provided solution is for when the web is a "Publishing Web", which is not the case here, so that solution won't do me any good.
Thanks in advance!
I have found out that the page IS a WikiPage after all. So I managed to change the content with following code:
using (var site = new SPSite(ApplicationResources.Url.SiteRoot))
{
using (var web = site.OpenWeb())
{
var page = web.GetFile(ApplicationResources.Url.FullDefaultPageName);
var item = page.Item;
item["Wiki Content"] = NewContent(title, text);
item.Update();
}
}
Maybe this is not the best method, so if anyone has a better and more reliable solution: feel free to correct me! ;)

Categories