+1-888-365-2779
Try Now
More in this section

Forums / Developing with Sitefinity / Creating and publishing GenericContent using API

Creating and publishing GenericContent using API

20 posts, 1 answered
  1. Chris
    Chris avatar
    87 posts
    Registered:
    11 May 2010
    05 Aug 2010
    Link to this post
    I am trying to create and publish a new GenericContent item using the API, but I cannot get it to publish. Here is my code...

    var manager = new ContentManager();
    var content = manager.CreateContent("text/html");
    content.Content = "Content";
    content.SetMetaData("Name""TestContent");
    manager.SaveContent(content);
    manager.PublishContent(content.ID)

    Am I doing something wrong?

    Thanks,

    Chris.
  2. Radoslav Georgiev
    Radoslav Georgiev avatar
    3370 posts
    Registered:
    01 Feb 2016
    09 Aug 2010
    Link to this post
    Hi Chris,

    Could you please specify exactly which version of Sitefinity are you using? I have tried with the same lines of code and they produced a publish content item in my module. After you save the content item you can try something like the bellow instead:
    StagedContent stagedCnt = (StagedContent)manager.GetStagedContent(content.ID);
    manager.SaveContent(stagedCnt, ContentStatus.Published);


    All the best,
    Radoslav Georgiev
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. Chris
    Chris avatar
    87 posts
    Registered:
    11 May 2010
    09 Aug 2010
    Link to this post
    Hi Radoslav,

    I am using version 3.7 SP3. I have tried that alternative code and get the same results. My content item is created as a draft which still has to be manually approved and published. Are there workflow restrictions which might mean I need to set the status to Approved before I can set it to Published or something?

    Chris.
  4. Radoslav Georgiev
    Radoslav Georgiev avatar
    3370 posts
    Registered:
    01 Feb 2016
    11 Aug 2010
    Link to this post
    Hello Chris,

    Thank you for getting mentioning that workflow is enabled. With workflow enabled the content life cycle is different and you have to go through the steps in the workflow process in order to publish the content item. You can take a look at this blog post for sample code on how to go through all steps of workflow programmatically.

    Sincerely yours,
    Radoslav Georgiev
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  5. Chris
    Chris avatar
    87 posts
    Registered:
    11 May 2010
    11 Aug 2010
    Link to this post
    That's great, thank you. However I am getting the following permissions error on activity.Execute(WorkflowRuntime.Instance) where activity is the Publish activity.


    System.Security.SecurityException: Permission denied.
       at Telerik.Security.Permissions.ApplicationPermission.ThrowSecurityException()
       at Telerik.Security.Permissions.ApplicationPermission.Demand()
       at Telerik.Cms.Engine.ContentWorkflowService.PublishContent(String providerName, Guid contenId)
    The action that failed was:
    Demand
    The type of the first permission that failed was:
    Telerik.Cms.Engine.Security.GlobalPermission
    The first permission that failed was:
    <IPermission class="Telerik.Cms.Engine.Security.GlobalPermission, Telerik.Cms.Engine, Version=3.7.2096.2, Culture=neutral, PublicKeyToken=dfeaee0e3978ac79" version="1" state="None" grant="64" deny="0"/>
    The demand was for:
    <IPermission class="Telerik.Cms.Engine.Security.GlobalPermission, Telerik.Cms.Engine, Version=3.7.2096.2, Culture=neutral, PublicKeyToken=dfeaee0e3978ac79" version="1" state="None" grant="64" deny="0"/>
    The assembly or AppDomain that failed was:
    Telerik.Security, Version=3.7.2096.2, Culture=neutral, PublicKeyToken=dfeaee0e3978ac79

    How can I set the correct permissions that are required for publishing GenericContent?
  6. Radoslav Georgiev
    Radoslav Georgiev avatar
    3370 posts
    Registered:
    01 Feb 2016
    11 Aug 2010
    Link to this post
    Hi Chris,

    It seems the user under which you are trying to run the code does not have permissions to publish the content item. You can try to log in with the administrator account and then run the code and see if it will break.

    Best wishes,
    Radoslav Georgiev
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  7. Chris
    Chris avatar
    87 posts
    Registered:
    11 May 2010
    11 Aug 2010
    Link to this post
    Hi Radoslav,

    My code is executing from a web service, is it possible to programatically simulate a "login" with an administrator account?

    Thanks,

    Chris.
  8. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    11 Aug 2010
    Link to this post
    Hello Chris,

    You can mimic the login by using FormsAuthentication.GetAuthCookie  and then set the cookie UserManager.Default.SetAuthenticationCookie(c); Also we check for HostingEnvironment and IsHosted property which gets a value indicating whether the current application domain is being hosted by an ApplicationManager object.

    Best wishes,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  9. Chris
    Chris avatar
    87 posts
    Registered:
    11 May 2010
    11 Aug 2010
    Link to this post
    Sorry Ivan, I don't understand the implications of the second part of your post. Does it require that I do something before attempting to publish the content?

    I have added the following code but still get the error:

    var authCookie = FormsAuthentication.GetAuthCookie("sv_admin", false);
    UserManager.Default.SetAuthenticationCookie(authCookie);

    sv_admin is the user name of an administrator account in my Site Finity website.
  10. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    17 Aug 2010
    Link to this post
    Hi Chris,

    ApplicationPermission.Demand() forces a SecurityException at run time if the current user has not been granted the permission specified by the current instance.

    1. If you use WCF services you need to enable AspNetCompatibility

    2. Then you have to log -in the user. Below is a sample code that would do this - create an authentication ticket for a user that has needed rights to Publish IContent item and then clear the authentication ticket.

    public void LoginUserForCurrentCall()
     {
         // you can pass the user as a  query string or use some decrypted format.
         MembershipUser user1 = Membership.Providers["Sitefinity"].GetUser("admin", false);
          if (user1 != null)
             {
                 var context = HttpContext.Current;
                 FormsAuthentication.SetAuthCookie(user1.UserName, true);
                 HttpCookie c = FormsAuthentication.GetAuthCookie(user1.UserName, true);
                 UserManager.Default.SetAuthenticationCookie(c);
                 // create content item here
                 // clear the authentication ticket after the content has been created.
                 FormsAuthentication.SignOut();
                  
            }
     }


    Regards,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  11. Chris
    Chris avatar
    87 posts
    Registered:
    11 May 2010
    18 Aug 2010
    Link to this post
    Hi Ivan,

    I'm not using WCF. This doesn't seem to work for me. How can I tell if the request has the right authentication? UserManager.GetCurrentUser().Identity.IsAuthenticated is returning false. 
  12. Chris
    Chris avatar
    87 posts
    Registered:
    11 May 2010
    24 Aug 2010
    Link to this post
    Unfortunately I am still able to get past this permissions error. I have checked that HostingEnvironment.IsHosted returns true.

    I am wondering if your sample code above is missing something as you have assigned HttpContext.Current to a variable but are not using it.
  13. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    24 Aug 2010
    Link to this post
    Hi Chris,

    1. Make sure that you are passing a valid CMS user to GetUser

    2. Check the current user right after you call SetAuthenticationCookie() by using UserManager.GetCurrentUser() or GetCurrentUserName()

    3. Make sure that the user you use have permissions to create content items.


    Sincerely yours,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  14. Chris
    Chris avatar
    87 posts
    Registered:
    11 May 2010
    24 Aug 2010
    Link to this post
    Hi Ivan,

    I am using a valid CMS user with permissions to publish (I can login to the site using this and publish content). When I call UserManager.GetCurrentUser() I get back a System.Web.Security.RolePrincipal with a GenericIdentity, however this Identity does not contain the user name (it's an empty string), AuthenticationType is also an empty string and Identity.IsAuthenticated is false.

    I can create, request approval, approve but not publish.
  15. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    25 Aug 2010
    Link to this post
    Hi Chris,

    You have to make 2 requests. The first request will create the authentication cookie and you should return it in the response. The second request should pass the cookie and you should Create, Publish etc items.

    Kind regards,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  16. Chris
    Chris avatar
    87 posts
    Registered:
    11 May 2010
    25 Aug 2010
    Link to this post
    Hi Ivan,

    Thanks for the suggestion but I am not sure how I can maintain a forms authenticated session with a web service. Are you able to point me towards some sample code for how I could achieve this? That would be really great.

    Thanks,

    Chris.
  17. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    25 Aug 2010
    Link to this post
    Hi Chris,

    In the first request you have to get the cookie. Here is a sample code that illustrates this.

    HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(uri);
     
    if (authCookie != null)
    {
        req.CookieContainer = new CookieContainer();
        req.CookieContainer.Add(authCookie);
    }
     
    WebResponse result = req.GetResponse();
    Stream ReceiveStream = result.GetResponseStream();
    Encoding encode = Encoding.GetEncoding("utf-8");
    StreamReader sr = new StreamReader(ReceiveStream, encode);
     
    string value = sr.ReadToEnd();
    sr.Close();

    After you have the cookie you need to create FormsAuthenticationTicket

    Regards,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  18. Chris
    Chris avatar
    87 posts
    Registered:
    11 May 2010
    25 Aug 2010
    Link to this post
    Hi Ivan,

    Thanks so much for your help! Following my last post I have been able to authenticate my request and I no longer get the error I mentioned previously.

    Here is my code:

    MembershipUser user = UserManager.Default.MembershipProvider.GetUser("admin", false);
    FormsAuthentication.SetAuthCookie(user.UserName, true);
    var authCookie = FormsAuthentication.GetAuthCookie(user.UserName, true);
    var ticket = FormsAuthentication.Decrypt(authCookie.Value);
    var identity = new FormsIdentity(ticket);
    Context.User = new RolePrincipal(identity);
    UserManager.Default.SetAuthenticationCookie(authCookie);

    After running this I am able to create, approve and publish in a single request.

    The only problem now is that if I look in the module my new content is locked. It seems to be published as I can include the content on a page and view it on the live site, but how do I unlock it?

    Thanks,

    Chris.
  19. Ivan Dimitrov
    Ivan Dimitrov avatar
    16072 posts
    Registered:
    25 Nov 2016
    25 Aug 2010
    Link to this post
    Hello Chris,

    Most probably you have used GetContent which locks the item. You have to use GetCurrentState method of ContentManager class which does not lock the item.

    Regards,
    Ivan Dimitrov
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
    Answered
  20. Chris
    Chris avatar
    87 posts
    Registered:
    11 May 2010
    25 Aug 2010
    Link to this post
    That's it! Thank you so much Ivan and Radoslav. Particularly for your patience in trying to get this to work :)

    Chris.
Register for webinar
20 posts, 1 answered