0

I'm having a problem with the delete function using ASP.NET mvc I can't seem to get how the delete works here. Should I create another method in the custom class for the delete? How does that work?

Here's my custom class that shows the item details

Custom.cs

public static Formular GetFormular(int itemId, ClientContext ctx, string listName)
        {
            var web = ctx.Web;
            var list = web.Lists.GetByTitle(listName);
            var formular = list.GetItemById(itemId);
            ctx.Load(formular,
                item => item["Title"],
                item => item["TemplateVersion"],
                item => item["TemplateNumber"]
            ctx.Load(formular.AttachmentFiles);
            ctx.ExecuteQuery();

            var model = new Formular
            {
                TemplateName = formular["Title"] as string != null ? formular["Title"] as string : string.Empty,
                TemplateVersion = formular["TemplateVersion"] as string,
                TemplateNumber = formular["TemplateNumber"] as string
            };

            return model;
        }

i then call this to my controller Controller

[SharePointContextFilter]
        public ActionResult Index(int? id)
        {

            User spUser = null;
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            int currentItem = id.Value;

            var spContext = SharePointContextProvider.Current.GetSharePointContext(HttpContext);

            using (var clientContext = spContext.CreateUserClientContextForSPHost())
            {
                if (clientContext != null)
                {
                    var queryString = HttpUtility.ParseQueryString(HttpContext.Request.Url.Query);
                    queryString.Remove("id");
                    ViewBag.CopyFormular = "./new/?id=" + currentItem + "&" + queryString;

                    var model = custom.GetFormular(currentItem, clientContext, "listName");
                    return View(model);

                }
            }

            return View();
        }
[SharePointContextFilter]
        public ActionResult Delete(int? id)
        {
            User spUser = null;
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            int currentItem = id.Value;
            var spContext = SharePointContextProvider.Current.GetSharePointContext(HttpContext);

            using (var clientContext = spContext.CreateUserClientContextForSPHost())
            {
                if (clientContext != null)
                {
                    var queryString = HttpUtility.ParseQueryString(HttpContext.Request.Url.Query);
                    queryString.Remove("id");
                    ViewBag.CopyFormular = "./new/?id=" + currentItem + "&" + queryString;

                    var model = Customizations.GetFormular(currentItem, clientContext, "listName");
                    model.Delete();

                }
            }

            return View();

1 Answer 1

0

I am not sure what you are trying to do here. Are you trying to delete the SharePoint list item using the MVC application? If yes, then read on:

The delete action is your code is only deleting the model. The GetFormular method, only fetches the sharepoint list item, then creates a model using its various properties and returns the model. There is no code to actually delete the list item from sharepoint.

To delete the item you should write a new method: DeleteFormular Try this code in the DeleteFormular method:

public static Formular DeleteFormular(int itemId, ClientContext ctx, string listName)
        {
            var web = ctx.Web;
            var list = web.Lists.GetByTitle(listName);
            var formular = list.GetItemById(itemId);
            formular.DeleteObject();
            ctx.ExecuteQuery();
           
        }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.