1

I have a Remote Event Receiver on the ItemAdding event of a list called clients. Everytime a new list item is created a new subsite is created.

However I want to make a validation, if site already exists (exception thrown by method createweb), then a message should be shown and form should stay so the user can correct the fields, however the page is redirected to AllItems.aspx

As you can see I am using SPRemoteEventResult with status and error message, but this seems to be useless, error is never shown anywhere.

 public SPRemoteEventResult ProcessEvent(SPRemoteEventProperties properties)
        {
            SPRemoteEventResult result = new SPRemoteEventResult();
            try
            {              
                switch (properties.EventType)
                {
                    case SPRemoteEventType.AppInstalled:
                        AppEvents.HandleAppInstalled(properties);
                        result.Status = SPRemoteEventServiceStatus.Continue;
                        break;
                    case SPRemoteEventType.AppUninstalling:
                        AppEvents.HandleAppUninstalled(properties);
                        result.Status = SPRemoteEventServiceStatus.Continue;
                        break;
                    case SPRemoteEventType.AppUpgraded:
                        AppEvents.HandleAppUpgraded(properties);
                        result.Status = SPRemoteEventServiceStatus.Continue;
                        break;
                    case SPRemoteEventType.ItemAdding:
                        if(properties.ItemEventProperties.ListTitle=="Clientes")
                        {
                            return Clients.CreateClientSiteAndUpdateSiteOnList(properties);                           
                        }
                        if (properties.ItemEventProperties.ListTitle == "Proyectos")
                        {
                            return Projects.CreateProjectSiteAndUpdateSiteOnList(properties);
                        }
                        break;  

                }
                return result;
            }
            catch (Exception ex)
            {
                //Log message
                result.ErrorMessage = "Capatech.Intranet:  " + ex.Message;
                result.Status = SPRemoteEventServiceStatus.CancelWithError;
                return result;
            }
        }



 public static SPRemoteEventResult CreateClientSiteAndUpdateSiteOnList(SPRemoteEventProperties properties)
        {
            SPRemoteEventResult result = new SPRemoteEventResult();
            try
            {
                using (ClientContext clientContext = TokenHelper.CreateRemoteEventReceiverClientContext(properties))
                {
                    if (clientContext != null)
                    {
                        var afterProperties = properties.ItemEventProperties.AfterProperties;
                        var beforeProperties = properties.ItemEventProperties.BeforeProperties;

                        string nombrecliente = afterProperties["Nombre_x0020_Cliente"].ToString();
                        int indexOfhash = nombrecliente.IndexOf("#");
                        int indexOfPipe = nombrecliente.IndexOf("|");
                        string clientName = nombrecliente.Substring(indexOfhash + 1, indexOfPipe - indexOfhash -1);

                        string site_title = afterProperties["NIT"].ToString() + " - " + clientName;
                        string site_url = afterProperties["NIT"].ToString();



                        //get the subweb clients which is the host of all client sites
                        clientContext.Load(clientContext.Web, w => w.Webs);
                        clientContext.ExecuteQuery();
                        var subWebClients = (from w in clientContext.Web.Webs where w.Title == "clientes" select w).SingleOrDefault();

                        Web newWeb = subWebClients.CreateWeb(site_title, site_url, site_title, "STS#0", 1033);
                        clientContext.Load(newWeb);
                        clientContext.ExecuteQuery();


                        result.ChangedItemProperties.Add("Sitio_x0020_Cliente", newWeb.Url + "," + site_title + " Sitio");
                        result.Status = SPRemoteEventServiceStatus.Continue;
                        return  result;
                    }
                    else
                    {
                        result.ErrorMessage = "No se pudo encontrar el contexto";
                        result.Status=SPRemoteEventServiceStatus.CancelWithError;
                        return result;
                    }
                }  
            }
            catch (Exception ex)
            {
                result.ErrorMessage = ex.Message;
                result.Status = SPRemoteEventServiceStatus.CancelWithError;
                return result;
            }                    
        }
  • I suspect your event receiver is not properly hooked up. Does the list exist on the host web, or in the app web? Are you able to step through the code with the debugger? – Derek Gusoff Jan 29 '15 at 22:10

Your Answer

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

Browse other questions tagged or ask your own question.