Exemple #1
0
        public void OnRemoteEvent(object sender, EventArgs args)
        {
            var remoteEventArgs = args as RemoteEventArgs;

            Log.Info($"Remote event raised: {remoteEventArgs.ItemName}", nameof(RemoteEventHandler));
            using (new SecurityDisabler())
            {
                Sitecore.Data.Database           masterDB = Sitecore.Configuration.Factory.GetDatabase("master");
                Sitecore.Data.Items.Item         home     = masterDB.GetItem("/sitecore/content/home");
                Sitecore.Data.Items.TemplateItem sample   = masterDB.Templates[new Sitecore.Data.ID("{76036F5E-CBCE-46D1-AF0A-4143F9B557AA}")];
                Sitecore.Data.Items.Item         myItem   = home.Add(remoteEventArgs.ItemName, sample);
            }
        }
Exemple #2
0
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            // Get master database

            // Get the context item from the master database (e.g. by ID)

            // Use a simple method of disabling security for the duration of your editing

            // Add a new comment item - use DateUtil.IsoNow to give it a timestamp name

            // Begin editing

            // Set the author and text field values from the form

            // End editing

            using (new Sitecore.SecurityModel.SecurityDisabler())
            {
                Sitecore.Data.Database   master   = Sitecore.Configuration.Factory.GetDatabase("master");
                Sitecore.Data.Items.Item comments = master.GetItem("/sitecore/content/HaiNguyen/Comments");

                Sitecore.Data.Items.Item templateItem = master.GetItem(new ID(TemplateReferences.COMMENT_TEMPLATE_ID));
                Sitecore.Data.TemplateID templateID   = new TemplateID(templateItem.ID);

                Sitecore.Data.Items.Item newComment = comments.Add(txtAuthor.Text, templateID);

                //TODO: eliminate SecurityDisabler if possible
                newComment.Editing.BeginEdit();
                try
                {
                    newComment.Name = txtAuthor.Text;
                    newComment["Comment Author"] = txtAuthor.Text;
                    newComment["Comment Text"]   = txtContent.Text;

                    LinkField link = newComment.Fields["Comment Author Website"];
                    link.Url      = txtLink.Text;
                    link.Text     = txtLink.Text;
                    link.Target   = "_blank";
                    link.LinkType = "external";

                    //TODO: update home
                    newComment.Editing.EndEdit();
                }
                catch (Exception ex)
                {
                    newComment.Editing.CancelEdit();
                }
            }
        }
    public void HowToCreateTemplateWithStandardValues()
    {
      var templateId = new Sitecore.Data.TemplateID(Sitecore.Data.ID.NewID);

      using (Sitecore.FakeDb.Db db = new Sitecore.FakeDb.Db
        {
          // create template with field Title and standard value $name
          new Sitecore.FakeDb.DbTemplate("Sample", templateId) { { "Title", "$name" } }
        })
      {
        // add item based on the template to the content root
        Sitecore.Data.Items.Item contentRoot = db.GetItem(Sitecore.ItemIDs.ContentRoot);
        Sitecore.Data.Items.Item item = contentRoot.Add("Home", templateId);

        // the Title field is set to 'Home'
        Xunit.Assert.Equal("Home", item["Title"]);
      }
    }
        protected Sitecore.Data.Items.Item GetOrCreateChild(
            Sitecore.Data.Items.Item parent,
            string childName,
            Sitecore.Data.Items.TemplateItem template,
            int sortOrder)
        {
            Sitecore.Data.Items.Item child = parent.Children[childName];

            if (child == null)
            {
                child = parent.Add(childName, template);

                using (new Sitecore.Data.Items.EditContext(child))
                {
                    child[Sitecore.FieldIDs.Sortorder] = sortOrder.ToString();
                }
            }

            return(child);
        }