I am using SharePoint Server 2007 Enterprise with Windows Server 2008 Enterprise. I have deployed a publishing portal. I am developing a ASP.Net web application using VSTS 2008 + C# + .Net 3.5 + ASP.Net + SharePoint Server 2007 SDK.
Here is my code snippets and I got error -- "Updates are currently disallowed on GET requests". Any ideas how to fix?
Microsoft.SharePoint.SPException: Updates are currently disallowed on GET requests. To allow updates on a GET, set the 'AllowUnsafeUpdates' property on SPWeb.
protected void Page_Load(object sender, EventArgs e)
{
try
{
SPWebApplication webApp = SPContext.Current.Site.WebApplication;
SPSiteCollection siteCollections = webApp.Sites;
foreach (SPSite item in siteCollections)
{
SPWeb webItem = item.OpenWeb();
webItem.AllowUnsafeUpdates = true;
}
SPSite newSiteCollection = siteCollections.Add("sites/myblog",
"New Sample Site", "New Sample Site Description", 1033, "BLOG#0",
"foo\\foouser", "Owner name", "owneremail#foo.com");
}
catch (Exception ex)
{
Response.Write(ex.ToString() + "\t" + ex.StackTrace);
}
}
The problem why you are not allowed to read/write to database on GET request is because your code will be exploitable via a cross-site scripting. Read about AllowUnsafeUpdates consequences here.
Anyway, if you like, you can set this SPWeb.AllowUnsafeUpdates to true, but use it like this:
try {
web.AllowUnsafeUpdates = true;
...
}
finally {
web.AllowUnsafeUpdates = false;
}
Add a check to ensure that you are getting a POST instead of a GET before you attempt to allow updates. Make sure that whatever is making the change does it via a POST request rather than using URL parameters and a GET.
if (IsPostBack)
{
...
}
Related
This might an odd question, but what I would like to achieve is a functionality that would allow specific users to view SharePoint pages as if they were logged in as different users.
Let's say we have a Student page and a Staff page. I am not a student, but I would like to log in as one to be able to view the Student page as a student would. Does that make sense? So, in a way, impersonation.
I have found some impersonation code, and it works fine, but it is not what I want. I was able to impersonate a user in a separate SPWeb object. But, how do I change the current user context of the active SPWeb object?
Here's what I've got:
private void ImpersonateUser()
{
string siteURL = "http://mywebsite/";
SPSite parentSite = new SPSite(siteURL);
SPUserToken systemToken = parentSite.SystemAccount.UserToken;
using (SPSite site = new SPSite(siteURL, systemToken))
{
using (SPWeb web = site.OpenWeb())
{
web.AllowUnsafeUpdates = true;
OpenUserContext(web, siteURL, #"domain\studentuser");
}
}
}
private void OpenUserContext(SPWeb web, string siteURL, string user)
{
try
{
SPUser ensure = web.EnsureUser(user);
SPSite impSite = new SPSite(siteURL, ensure.UserToken);
SPWeb impWeb = impSite.OpenWeb();
// Do something as impersonated user
label1.Text = "Currently logged in as: " + impWeb.CurrentUser.ToString() + "(" + impWeb.CurrentUser.Name + ")";
}
catch (Exception ex) { label1.Text = ex.Message + "<br>" + user; }
}
Thanks a lot.
While SharePoint does allow your code to interact with it while impersonating a user, it has no support to allow you to browse and view things as if you were another user.
You can, however, code your customizations to take in to account your desire and add the impersonation logic to them (but that's up to you to implement and SP will not help you there). Project Server does something like what you are asking but only when it comes to Project Server owned elements.
If your impersonation code is executing behind a custom .aspx page in SharePoint, you can programmatically add an xsltlistviewwebpart to the page to display one or more list views under your impersonated context.
I am creating a web app. where I want be able to incorporate Google Maps into 1 of my pages.
From what I have read in other places, the easiest think is to place a web browser onto the form but there is no 'Web Browser' in the tool-box.
What I am trying to do is to insert a location into a textbox(ie. London) and insert a type of sport(ieCycling) and the resultant map shows up. Is there any other way in doing this in C# other than using the web browser tool.
Here is my code:
protected void btnSearch_Click(object sender, EventArgs e)
{
string sport = txtSport.Text;
string location = txtLocation.Text;
try
{
StringBuilder queryAddrress = new StringBuilder();
queryAddrress.Append("https://maps.google.ie/");
if (sport != string.Empty)
{
queryAddrress.Append(sport+","+"+");
}
if (location != string.Empty)
{
queryAddrress.Append(location + "," + "+");
}
Panel1.Navigate(queryAddrress.ToString());
}
catch (Exception ex)
{
MessageBox.Show(ex.Message.ToString(),"Error");
}
} protected void btnSearch_Click(object sender, EventArgs e)
{
string sport = txtSport.Text;
string location = txtLocation.Text;
try
{
StringBuilder queryAddrress = new StringBuilder();
queryAddrress.Append("https://maps.google.ie/");
if (sport != string.Empty)
{
queryAddrress.Append(sport+","+"+");
}
if (location != string.Empty)
{
queryAddrress.Append(location + "," + "+");
}
Panel1.Navigate(queryAddrress.ToString());
}
I tried to put the address into a panel but this is clearly wrong. Any help would be greatly appreciated!
It seems like you are very confused and mixing ASP.NET Web Forms with Windows Forms.
Specifically, MessageBox.Show() would open a Windows message box, not a browser window. And it would happen on the server side for whatever user your web server runs as. Probably not the desired intention. Also, you can't "put a web browser" onto a page. There is a WebBrowser control for Windows Forms, which embeds a minified version of Internet Explorer into a Windows application. But again, probably not what you want.
ASP.NET can be used as if it were just a normal HTML site. So find some Google Maps tutorials for HTML and follow those.
I have a small C# console program that retrieves an item list from quickbooks, and I am trying to figure out how to expose that data to Microsoft Access. It is in XML format.
I want to retrieve the data in real-time, since it only takes about a second to get the data, whenever Access calls for it. I am using Access 2003 and VS 2010.
If there is a way to do this with VBA that would work fine as well. I can get the XML data using VBA already, but I don't know how to go from there.
Here is the code I use in C#:
public string DoQBQuery(XmlDocument doc)
{
bool sessionBegun = false;
bool connectionOpen = false;
RequestProcessor2 rp = null;
string ticket = "";
try
{
//Create the Request Processor object
rp = new RequestProcessor2();
//Connect to QuickBooks and begin a session
rp.OpenConnection2("", "QB Transaction Item Retriever", QBXMLRPConnectionType.localQBD);
connectionOpen = true;
ticket = rp.BeginSession("", QBFileMode.qbFileOpenDoNotCare);
sessionBegun = true;
//Send the request and get the response from QuickBooks
string responseStr = rp.ProcessRequest(ticket, doc.OuterXml);
//End the session and close the connection to QuickBooks
rp.EndSession(ticket);
sessionBegun = false;
rp.CloseConnection();
connectionOpen = false;
return responseStr;
}
catch (Exception e)
{
MessageBox.Show(e.Message, "Error");
if (sessionBegun)
rp.EndSession(ticket);
if (connectionOpen)
rp.CloseConnection();
throw;
}
}
"I am trying to figure out how to expose that data to Microsoft Access. It is in XML format"
I've never tried this before, but I think this is possible by putting the c# classes that accomplish what you discussed in a dll and calling from Access. Here is a post from this site about that.
A Simple C# DLL - how do I call it from Excel, Access, VBA, VB6?
P.S. Given I've never tried this before, I would have put this in the comment section of your original post, but I'm new to this site and am unable to do that.
I'm trying to code a Remote Desktop Application using C# .NET.
I followed some examples (listed below) and created a Windows Form, added references to MSTSLib, added Microsoft Terminal Service Control to the Form and code the following behavior:
namespace RDConnector
{
public partial class Form1 : Form
{
const string server = "55.55.55.555";
const string userNameBase = "username";
const string passwordBase = "password";
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
try
{
rdClient.Server = server;
rdClient.UserName = userNameBase;
/*IMsTscNonScriptable secured = (IMsTscNonScriptable)rdClient.GetOcx();
secured.ClearTextPassword = passwordBase;*/
rdClient.AdvancedSettings8.DisplayConnectionBar = true;
rdClient.AdvancedSettings8.ClearTextPassword = passwordBase;
rdClient.AdvancedSettings8.EncryptionEnabled = -1;
//// Start connection
rdClient.Connect();
MessageBox.Show("Connection Status + " + rdClient.Connected.ToString());
}
catch (Exception Ex)
{
MessageBox.Show("Exception ocurred: " + Ex.Message);
}
}
}
}
However, it isn't working, the Connection status after calling rdClient.Connect() is 2 (0 before calling it), but nothing happens. I also ran the Example 1 and it doesn't work.
I'm using Windows 7 - 64 bit and Visual Studio C# Express. Visual Express Edition doesn't have a X64 compiler, Could be the problem related with that?
I'll really appreciate your help.
Examples:
http://www.codeproject.com/KB/cs/RemoteDesktop_CSharpNET.aspx
Running COM component controls on multiple threads
I found the problem at last. When you want to choose COM Components, just select "Microsoft RDP Client Control - version x". I choose the 8 version that works perfectly for me.
I'm getting this error while trying to post data to a hosted web application from a windows service.
PostSubmitter post = new PostSubmitter();
post.Url = "http://192.168.0.1/Invoice/Invoice1.aspx";
post.PostItems.Add("subscriberid", subscriberid.ToString());
post.PostItems.Add("StartDate", StartDate);
post.PostItems.Add("EndDate", EndDate);
post.PostItems.Add("AdvanceBillDate", AdvanceBillDate);
post.Type = PostSubmitter.PostTypeEnum.Post;
try
{
string res = post.Post();
}
catch (Exception exp)
{
}
This is code snippet of my windows service which posts data to web application.
Does any one know the reason.I'm using asp .Net C#
Compare your request from C# with one done in a browser.
Use fiddler to do this.
You should be able to compare everything from header values, to complete post data, etc. and be able to figure out what you have missing. I would suspect you are leaving out required a value and the server application is throwing a (likely unexpected) exception.
Finally i got wat was missing.Actually i was posting data to the web application and reading it using Request.QueryString......Which is actually how Get Method is read.So modified my code as
PostSubmitter post = new PostSubmitter();
post.Url = "http://192.168.0.1/Invoice/Invoice1.aspx";
post.PostItems.Add("subscriberid", subscriberid.ToString());
post.PostItems.Add("StartDate", StartDate);
post.PostItems.Add("EndDate", EndDate);
post.PostItems.Add("AdvanceBillDate", AdvanceBillDate);
post.Type = PostSubmitter.PostTypeEnum.Get;
try
{
string res = post.Post();
}
catch (Exception exp)
{
}