C# Windows form App with SharePoint - c#

Im new to the whole C# and Visual Studio program.
I have been given a task to create a windows form app using C# to connect to a SharePoint site and retrieve any data from there e.g. list, files.
I have designed my application so that you can manually add the site url which you want then click a button and this will generate a xml file with all the data which is in the site.
This is the way i need my xml to be set out:
The report should be in either csv or xml format e.g.
<SiteCollection Name=”SiteCollection”>
<Web Name=”Web Name”>
<Library Name=”Library Name”>
<Document Name=”DocName1”/>
<Folder Name=”Folder Name”>
<Document Name=”DocName2”/>
<Document Name=”DocName3”/>
</Folder>
</Library>
<List Name=”List Name”>
<Web Name=”Web Name”>
<Library Name=”Library Name”>
<Document Name=”DocName1”/>
<Folder Name=”Folder Name”>
<Document Name=”DocName2”/>
<Document Name=”DocName3”/>
</Folder>
</Library>
<List Name=”List Name”>
</Web>
</Web>
</SiteCollection>
This is how I have my code for my app set out so far:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
namespace SharePoint.College
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void btn1_Click(object sender, EventArgs e)
{
}
}
}
Im just looking for some advice and some help to start this off.
Thanks for any replies

As your new to this you should start with a few basic tutorials, a search for C# tutorials will turn up loads of results. You could start with MSDN C# Tutorials.
Once you are comfortable with C# take a look at some Winforms tutorials such as C# Corner - Tutorial: Working with Windows Forms - Part I
Assuming you are using SharePoint 2010 and you are not going to be running this code directly on the SharePoint server you will want to look at Using the SharePoint Foundation 2010 Managed Client Object Model in order to retrieve the information.
As a side note you will get better responses from this site if you do this kind of research first and then ask questions (with relevant code snippets) about specific difficulties you are encountering, see What types of questions should I avoid asking? on the help pages.

This code should give you access to a List or Library. Note that since you are a beginner, you may not be familiar with handling SPList so I have converted to a DataTable which I believe should be easier. Note that I have named the member variables (those starting with this.xxx)to indicate what they are/should contain.
using (SPSite site = new SPSite(this.siteUrl))
{
using (SPWeb web = site.OpenWeb(this.siteName))
{
SPList mylib = web.Lists[this.libraryName];
DataTable dt = mylib.Items.GetDataTable();
}
}
In the mean time please follow Aquila Sands suggestions, it pays off in the long run if you decide to stay with C# and SharePoint. Good luck.

Related

How to embed a R plot in a Winform in c#?

I am using R.NET to perform computation in my C# application and
now I'd like to display the results in a Winform.
Anyone could advise on how to embed a R plot in a winform using R.NET ?
I found the below post which seems outdated as I can't find any reference nor Nuget package for the RNETGraph namespace that they use. The link referenced in the post have also been archived.
display multiple R Embedded Graph in multiple panel winform c#
And I would like to avoid the ugly solution of saving the image and then loading it in a PictureBox as I need to change the plot dynamically according to user input.
Thanks
You can use Dieter Menne's RGraphHooks to display R's plot output in a graphical WinForms element (e.g. a Windows.Forms.Panel). RGraphHooks has a dependency to Dino Esposito's Win32 hooks library.
Usage of RGraphHooks is quite simple. See this blog post by Peter Dai Dinh for a small demo program.
What you basically do is, you attach an RGrapHook to a specific control in your WinForms GUI and then wrap your engine.Evaluate("plot(...)") in this hook:
RGraphAppHook cbt = new RGraphAppHook { GraphControl = panelForPlot };
cbt.Install();
engine.Evaluate("plot(rnorm(100))");
cbt.Uninstall();
I never got hold of R.NET - documentation just wasn't very clear.
There is another option, however. You can use the command line to transfer arguments from your C# application to your R.Script.
For example:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApp2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string strCmdText;
strCmdText = "Rscript.exe [directory here]\\script.R 10 arg2"; //what comes after script.R are the arguments you are passing.
System.Diagnostics.Process.Start("CMD.exe", strCmdText);
}
}
}
Then, it is very easy to retrieve the arguments in the R Script. Just use:
args <- commandArgs(trailingOnly = TRUE)
var1 <- args[1] #Argument 1
var2 <- args[2] #Argument 2
Addendum: Please note, you should have your RScript.exe in the environment variables in order to get the above to work.

Form showing nothing. using CefSharp with Windows form in C#

I am trying to show a webpage in a form using C#. I am using CefSharp to show the webpage (as I would like to test & learn how it works). But since I have worked only on inbuilt webbrowser, I have no idea how to get started with CefSharp(Finding it difficult to get any tutorials). I tried to write this code which executes but the form shows nothing in it. Where am I going wrong ?
Here is my Code :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CefSharp;
using CefSharp.WinForms;
namespace chrometest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
Cef.Initialize(new CefSettings());
var test = new CefSharp.WinForms.ChromiumWebBrowser("http://www.google.com")
{
Dock = DockStyle.Fill,
};
this.Controls.Add(test);
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
Edit :
I tried to run the example from https://github.com/cefsharp/CefSharp/tree/cefsharp/41/CefSharp.WinForms.Example
When I try to build the solution, it shows a dialog box saying "Restoring Nuget Package cef.redist.x64 3.2454.1317" with a progresss bar which takes hell lot of time to complete but never completes and after sometime it hangs.
Please help what should I do to get the example running.
The CefSharp project has a few different examples as part of the main project.
Basic Example using Nuget
https://github.com/cefsharp/CefSharp.MinimalExample
More Advanced Examples
https://github.com/cefsharp/CefSharp/tree/cefsharp/41/CefSharp.WinForms.Example
When you installed the project using Nuget it should have opened a Readme.txt file, it contains a lot of useful information.
https://github.com/cefsharp/CefSharp/blob/cefsharp/41/NuGet/Readme.txt
In the context of WinForms there's a few tutorials
http://www.codeguru.com/columns/dotnet/if-you-like-it-put-an-html5-ui-on-it.html
http://thechriskent.com/2014/08/18/embedded-chromium-in-winforms/
For those reading this looking for WPF, there's
http://www.codeproject.com/Articles/881315/Display-HTML-in-WPF-and-CefSharp-Tutorial-Part
http://www.codeproject.com/Articles/887148/Display-HTML-in-WPF-and-CefSharp-Tutorial-Part
More Links
https://github.com/cefsharp/CefSharp.Tutorial
http://thechriskent.com/category/net/cefsharp/

linking a new windows form with an existing project in c#

I have a c# problem. I have created a project called 'classproject' and I have added several forms to project successfully and successfully ran the program, but there is this other windows form that I just added and when I double-clicked on the form, it took me to the code view and brought out this:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class demo : Form
{
public demo()
{
InitializeComponent();
}
private void demo_Load(object sender, EventArgs e)
{
}
}
}
Now, my challenge is that the name of the project is meant to show by default after the namespace as in "namespace classproject" instead of "namespace WindowsFormsApplication1". Please can anyone help me out, what am I meant to do because have tried everything possible. Even if I change the name myself its still going to flag error... someone please help me as soon as possible.
I'm not sure exactly what your issue is. If you change the namespace manually in the code to classproject it should be fine. What error are you talking about? You would also need to change the designer code file as well (probably called demo.Designer.cs).
Just a side note as well. Standard naming convention for C# is to use Pascal-case (as in ClassProject).
You must change namespace in ALL files that was generated by Visual Studio. You can do it manually, or just set cursor on "WindowsFormsApplication1", press F2 and enter new name.
Optionaly you can also change namespace in project properties on Application tab - then all new files added to project will also have namespace that you set there.

ASP.Net and Web Services

I have been searching for 4 days non stop. I am sleep deprived and going crazy. Can someone please help me or at least tell me what I'm doing wrong. This is my project
Develop a client web page app that uses the web service found at http://www.marksmerry.com/peanutbutter/WebService1.asmx.
The service generates a random number m
This service receives a guess , an integer between 1-100 inclusive. It returns a string:
low - if the guess is lower than m
equal – if the guess is correct
high - if the guess is higher than m
I have referenced the web service but I'm lost at the syntax or something please help me! This is what I have so far.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using localhost;
using System.Web.Services;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
localhost.WebService1 ws1 = new WebService1();
//What goes in this area. I have been searching and have tried all kinds of combination all have resulted in build errors
}
}
type ws1. and a list of methods will appear.
any service methods that were discovered by visual studio when you referenced the 'peanutbutter' webservice will be available to call on the proxy class (called WebService1 in your code example).
string result = ws1.Guess(42);
If you just type the url in the browser, it wil show you what methods it has.
http://www.marksmerry.com/peanutbutter/WebService1.asmx
I can see a web method Guess which takes an int.
As per your code, You can call it via
string result = ws1.Guess(10); // or input
When you added the reference you should have got the chance to give it a name. It's only semantics but it might be better to give it a different name from localhost.
Previous commentators have made good suggestions so I'd follow them.
Only thing I'd suggest is this.
string result = ws1.Guess("10"); //EDIT: this is wrong of course. It takes an int.
I did some work this morning using a web service.
myCoService.Service1 v24 = new myCoService.Service1();
System.Xml.XmlNode doc = v24.CreateSite(newSiteName);
Should be as simple as that.
If it isn't I'd have a look again at how you set up your web reference. Also please let us know what NET framework you are using.
I added a web Reference to a test project and a button on a page which fires this event.
protected void PeanutGuess_click(object sender, EventArgs e) {
PeanutButter.WebService1 pb = new PeanutButter.WebService1();
string response = pb.Guess(10);
lblResult.Text = string.Format("Response for 10 is " + response);
}
This works fine for me. I'm using VS2010 and the project uses Net Framework 3.5

How to obtain Handle.ToInt32() in an ASP.NET web application

I am trying to learn and use an SDK for a vendor's product. Unfortunately, the documentation is sketchy and I've run into a void in my own knowledge of the .Net Framework.
I have some working code for a windows forms application and I am trying to get it working in an ASP.NET web form app. The vendor documentation implies you can do this but maybe you cannot..
Snippet from the working windows app:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using TRIMSDK;
private void ConnectUserBtn_Click(object sender, System.EventArgs e)
{
Database db = new Database();
Databases dbChooser = new Databases();
IDatabase dbI = dbChooser.ChooseOneUI(Handle.ToInt32());
if (dbI == null)
{
return;
}
db.Id = dbI.Id;
Now here is my attempt inside click event handler for an .ASPX page:
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TRIMSDK;
protected void ConnectUserBtn_Click(object sender, EventArgs e)
{
Database db = new Database();
Databases dbChooser = new Databases();
IDatabase dbI = dbChooser.ChooseOneUI(Handle.ToInt32());
if (dbI == null)
{
return;
}
I get a compile complaint in the line just above that reads "The name 'Handle' does not exist in the current context.
This part of the SDK I am trying to use displays various modal dialogs that reflect the properties of the product to facilitate "client" development. I fear it might be only "Windows clients" and that ASP.NET web apps cannot do this.
Is there something I can add to resolve this?
For web apps, modal dialogs would be done at the client, usually via javascript and dhtml - not at the server (where ASP.NET code executes). So I fear that this product is indeed winforms only.
Just pass it:
int hwnd = 0;
IDatabase dbI = dbChooser.ChooseOneUI(hwnd);

Categories

Resources