function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
ShaikAShaikA 

How to Insert FeedItem as a sites guest user?

Hi All,

Please let me know, how to insert feeditem for site user, i am getting error like 
DmlException: Insert failed. First exception on row 0; first error: CANNOT_INSERT_UPDATE_ACTIVATE_ENTITY, Entity is read-only: FeedItem: []

Vf page publish in sites:
<apex:page controller="fileUploadController">
<apex:messages />
<apex:form id="frm">
    <apex:pageblock >
        <apex:pageblocksection columns="1">
             <apex:inputFile value="{!file}" filename="{!conVers.title}"/>
             <apex:commandbutton action="{!upload}" value="Upload" />
        </apex:pageblocksection>
    </apex:pageblock>   
</apex:form>
</apex:page>
Class:
public with sharing class fileUploadController{
public blob file { get; set; }
public ID accid{get;set;}
public ContentVersion conVers{get;set;}

 public fileUploadController(){
     accid=ApexPages.currentPage().getParameters().get('id');
     conVers= new ContentVersion();
 }
 public PageReference upload(){
  
        conVers.versionData = file;
        conVers.pathOnClient ='/'+conVers.Title;
        try
        {
        insert conVers;
        FeedItem elm = new FeedItem(Body = 'Post with related document body', ParentId = accid, RelatedRecordId =conVers.Id, Type = 'ContentPost');
        insert elm;
        }
        catch (DMLException e)
        {
        ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,'Error uploading File'));
        return null;
        }
        finally
        {
        conVers= new ContentVersion();
        }
        ApexPages.addMessage(new ApexPages.message(ApexPages.severity.INFO,'File uploaded successfully'));
        return null;
    }
}

Thanks in Advance
Shaik 
sagarika bsagarika b
Hi Shaik,

The problem in your code is  "ParentId of FeedItem,"
 
Please check below link for inserting a FeedItems, and it works for me.
.User-added image
Hope it will be helpful

BestRegrads
Sagarika
 
ShaikAShaikA
Hi Sagarika,

Thanks for your replay, Have u tried it as site user or internal user.
for internal user, it's working fine but as site user it's throwing error. please share  the code.

Reagards,
Shaik
sagarika bsagarika b
Hi Shaik,

Sorry for the inconvenience, I just tried it as the internal user only.

As you know the Site user can not update the FeedItem object. It will not be possible to get the desired functionality as of now. 
However, you can log an Idea on our Idea Exchange to open up the access to this object in future releases. Please visit at:
 http://success.salesforce.com/ideaHome?c=09a30000000D9xtAAC  (http://success.salesforce.com/ideaHome?c=09a30000000D9xtAAC)

We hope it will be helpful.
Thanks
Sagarika
ShaikAShaikA
Thanks for your update Sagarika.