0

I'm trying to make a http request from a C# feature for sharepoint. The request is made inside "ItemAdded" function of a List.

The request is made like this:

   public override void ItemAdded(SPItemEventProperties properties) {
        base.ItemAdded(properties);

        EventFiringEnabled = false;

        string currentSite = SPContext.Current.Web.Url;

        using (SPSite spSite = new SPSite(currentSite)) {
            using (SPWeb spWeb = spSite.OpenWeb()) {
                SPSecurity.RunWithElevatedPrivileges(delegate () {
                    try {
                        HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://www.google.com");

                        SPList l = spWeb.Lists["d.3_List"];
                        SPListItem item = l.Items.Add();
                        item["Title"] = "Success";
                        item.Update();

                    } catch (Exception ex) {
                        SPList l = spWeb.Lists["d.3_List"];
                        SPListItem item = l.Items.Add();
                        item["Title"] = "Exception: " + ex;
                        item.Update();
                    }

                });
            }
        }

        EventFiringEnabled = true;
    }

But it always fails with the following error: Request error

Does anyone know how to fix that?

0

What you can do is create a delegate that runs with elevated privileges within your function. This should allow you to run it. Give this a try:

EDITED: Give this a shot.

public override void ItemAdded(SPItemEventProperties properties) {
    base.ItemAdded(properties);
    string currentSite = SPContext.Current.Web.Url;
    EventFiringEnabled = false;
    using (SPSite spSite = new SPSite(currentSite))
    {
        using (SPWeb spWeb = spSite.OpenWeb())
        {
            SPSecurity.RunWithElevatedPrivileges(delegate () {
                try {
                    HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://www.google.com");
                } catch (Exception ex) {
                    Console.WriteLine("Error: " + ex.StackTrace);
                }

            });
        }
    }
    EventFiringEnabled = true;
}
  • When I add this to my code it just skips the whole block, doesn't even enter breakpoints... – Hilfsschueler Apr 2 at 12:20
  • make sure you put in the right place. If you post more of your code, I might have a better idea of what is going on. – lazoDev Apr 2 at 12:36
  • I added my function to the question as it is now – Hilfsschueler Apr 2 at 13:00
  • You have to open the Site and Web so the Object Model knows what you are giving permissions to, to use the delegate. I will revise my answer – lazoDev Apr 2 at 13:22
  • Sadly it is still not working. It just skips over everything, even breakpoints. I added my code again as it is now. Somehow Console.WriteLine() is not working, that's why I'm trying to write the results to a list. – Hilfsschueler Apr 2 at 13:42
0

I fixed it, the problem was that I ran the feature in a sandboxed solution.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.