Trying to get a Blogpost File URL in an event giving object reference error

we tried the below code and ended up with an object reference error

private void Events_AfterUpdateContent(ContentAfterUpdateEventArgs e)
		{
			try
			{
				var content = _contentApi.Get(e.ContentId, e.ContentTypeId);
				var strHtml = content.HtmlName("Web") + " " + GetContent(content) + " " + Tags(e.ContentId, e.ContentTypeId);
				if (Checkcontent(e.ContentTypeId))
				{
					bool isAbused = ValidateContent(strHtml);

					if (isAbused) _abuseController.IdentifyAsAbusive(e.ContentId, e.ContentTypeId);
				}
			}
			catch (Exception)
			{
				throw;
			}
		}

private string GetContent(Content content)
		{

			if(content.ContentTypeId == Apis.Get<IBlogPosts>().ContentTypeId)
			{
				var post = Apis.Get<IBlogPosts>().Get(content.ContentId);
				string body = post.Body("Web");
				Apis.Get<IEventLog>().Write("fileurl " + post.File.FileUrl, new EventLogEntryWriteOptions { });
				body = body + " <img alt=" " src="" + post.File.FileUrl + "" />";
				return body;
			}
			return content.HtmlDescription("Web");
		}

We are getting object references on the line where we are accessing the code  Apis.Get<IEventLog>().Write("fileurl " + post.File.FileUrl, new EventLogEntryWriteOptions { });
  we are uploading the post image while creating the blogpost.

we tested this on 11.1.3 and 11.1.5 , still we faced same issues on both the versions.

Parents
  • post.File would be for the attachment on a blog post.  post.PostImageFile would be for the post image.

  • Yes, but both are throwing object reference errors while accessing with the above code, still, if we attach a post attachment as well as post image

  • How are you creating the blog post? Are the images showing as expected in the UI when created? Or are you creating the blog post in code too?

  • We are creating the blog post using the create blog post page, The images are shown as expected on the UI. 

  • Can you give the full stack trace of the exception? Are there any other exceptions in the log that may be related?

    Can you debug step through your GetContent method and inspect the File/PostImageFile properties?

  • The File Properties is always returning null if we uploaded both file and post images. 

    {
       "Id":111,
       "Title":"adfasdf",
       "Slug":"adfasdf 1476313547",
       "Url":"http://saicommunity.wan.com/new-custo/b/blog/posts/adfasdf-1476313547",
       "TitleUrl":null,
       "PublishedDate":"2021-06-15T16:06:00",
       "ExcerptSize":250,
       "UsePostSummary":true,
       "GeneratePostSummary":true,
       "PostImageFile":{
          "Date":null,
          "ContentType":"image/gif",
          "FileName":"I51s6a.gif",
          "FileSize":null,
          "FileUrl":"http://saicommunity.wan.com/cfs-filesystemfile/__key/communityserver-blogs-components-weblogfiles/00-00-00-00-09/I51s6a.gif?_=637593515795813771",
          "IsRemoteAttachment":false,
          "Warnings":[
             
          ],
          "Errors":[
             
          ]
       },
       "PostImageUrl":"~/cfs-file/__key/communityserver-blogs-components-weblogfiles/00-00-00-00-09/I51s6a.gif",
       "Excerpt":"sdfasdfsd cssdsd",
       "BlogId":9,
       "GroupId":7,
       "IsApproved":true,
       "HasPendingChanges":false,
       "IsPostEnabled":true,
       "IsExternal":false,
       "EnableCrossPosting":false,
       "IsLocked":false,
       "Views":0,
       "Author":{
          "Id":2108,
          "IsSystemAccount":false,
          "Username":"testuser1",
          "DisplayName":"testuser1",
          "Password":null,
          "Points":1765,
          "QualityPercentile":14,
          "PrivateEmail":"test1@test1.com",
          "IsIgnored":null,
          "ForceLogin":null,
          "BanReason":"Other",
          "BannedUntil":"0001-01-01T00:00:00",
          "JoinDate":"2021-04-30T16:37:52.84",
          "LastVisitedDate":"2021-06-15T16:24:35.06",
          "LastLoginDate":"2021-06-15T14:59:48.16",
          "TermsOfServiceAcceptanceDate":null,
          "RequiresTermsOfServiceAcceptance":false,
          "AllowSiteToContact":false,
          "AllowSitePartnersToContact":false,
          "EnableEmail":true,
          "EnableHtmlEmail":true,
          "EnableTracking":true,
          "EnableConversationNotifications":true,
          "EnableCommentNotifications":true,
          "AccountStatus":"Approved",
          "EnableFavoriteSharing":true,
          "ConversationContactType":"FriendsOnly",
          "PostSortOrder":"Ascending",
          "EditorType":"",
          "EnableDisplayInMemberList":true,
          "EnableCollapsingPanels":true,
          "EnableDisplayName":true,
          "EnableUserSignatures":true,
          "HasPassword":true,
          "ModerationLevel":"Unmoderated",
          "RssFeeds":[
             
          ],
          "TotalPosts":73,
          "Presence":"Online",
          "EnablePresenceTracking":true,
          "TimeZone":5.5,
          "TimeZoneInfo":{
             "Id":"India Standard Time",
             "DisplayName":"(UTC+05:30) Chennai, Kolkata, Mumbai, New Delhi",
             "StandardName":"India Standard Time",
             "DaylightName":"India Daylight Time",
             "BaseUtcOffset":"05:30:00",
             "AdjustmentRules":null,
             "SupportsDaylightSavingTime":false
          },
          "TimeZoneId":"India Standard Time",
          "EnablePostPreviewPopup":false,
          "EnableEmoticons":true,
          "DateFormat":"d MMM yyyy",
          "ProfileFields":[
             {
                "Label":"AOL IM",
                "Value":"",
                "Warnings":[
                   
                ],
                "Errors":[
                   
                ]
             },
             {
                "Label":"Blog",
                "Value":"",
                "Warnings":[
                   
                ],
                "Errors":[
                   
                ]
             },
             {
                "Label":"Department",
                "Value":"",
                "Warnings":[
                   
                ],
                "Errors":[
                   
                ]
             },
             {
                "Label":"Education",
                "Value":"",
                "Warnings":[
                   
                ],
                "Errors":[
                   
                ]
             },
             {
                "Label":"Email",
                "Value":"",
                "Warnings":[
                   
                ],
                "Errors":[
                   
                ]
             },
             {
                "Label":"Facebook",
                "Value":"",
                "Warnings":[
                   
                ],
                "Errors":[
                   
                ]
             },
             {
                "Label":"Gallery",
                "Value":"",
                "Warnings":[
                   
                ],
                "Errors":[
                   
                ]
             },
             {
                "Label":"Google IM",
                "Value":"",
                "Warnings":[
                   
                ],
                "Errors":[
                   
                ]
             },
             {
                "Label":"Hobbies",
                "Value":"",
                "Warnings":[
                   
                ],
                "Errors":[
                   
                ]
             },
             {
                "Label":"Mobile",
                "Value":"",
                "Warnings":[
                   
                ],
                "Errors":[
                   
                ]
             },
             {
                "Label":"Occupation",
                "Value":"",
                "Warnings":[
                   
                ],
                "Errors":[
                   
                ]
             },
             {
                "Label":"Office Phone",
                "Value":"",
                "Warnings":[
                   
                ],
                "Errors":[
                   
                ]
             },
             {
                "Label":"Office",
                "Value":"",
                "Warnings":[
                   
                ],
                "Errors":[
                   
                ]
             },
             {
                "Label":"Title",
                "Value":"",
                "Warnings":[
                   
                ],
                "Errors":[
                   
                ]
             },
             {
                "Label":"Twitter",
                "Value":"",
                "Warnings":[
                   
                ],
                "Errors":[
                   
                ]
             },
             {
                "Label":"Windows Live IM",
                "Value":"",
                "Warnings":[
                   
                ],
                "Errors":[
                   
                ]
             },
             {
                "Label":"Yahoo IM",
                "Value":"",
                "Warnings":[
                   
                ],
                "Errors":[
                   
                ]
             },
             {
                "Label":"core_Birthday",
                "Value":"",
                "Warnings":[
                   
                ],
                "Errors":[
                   
                ]
             },
             {
                "Label":"core_Gender",
                "Value":"0",
                "Warnings":[
                   
                ],
                "Errors":[
                   
                ]
             },
             {
                "Label":"core_Public Email",
                "Value":"",
                "Warnings":[
                   
                ],
                "Errors":[
                   
                ]
             },
             {
                "Label":"core_Language",
                "Value":"en-US",
                "Warnings":[
                   
                ],
                "Errors":[
                   
                ]
             },
             {
                "Label":"core_Website",
                "Value":"",
                "Warnings":[
                   
                ],
                "Errors":[
                   
                ]
             },
             {
                "Label":"core_Location",
                "Value":"",
                "Warnings":[
                   
                ],
                "Errors":[
                   
                ]
             }
          ],
          "EnabledActivityMessageTypes":[
             "NewUser",
             "NewBlogPost",
             "NewForumThread",
             "NewMediaGalleryPost",
             "NewGroup",
             "NewGroupMember",
             "NewFriendship",
             "NewUserAvatar",
             "NewWikiPage",
             "UpdatedWikiPage",
             "ThirdPartyMessageType",
             "Status"
          ],
          "ExtendedAttributes":[
             
          ],
          "Language":"en-US",
          "Location":"",
          "PublicEmail":"",
          "WebUrl":"",
          "Signature":"",
          "SignatureFormatted":"<div style=\"clear:both;\"></div>",
          "Gender":"NotSet",
          "ProfileUrl":"/members/testuser1",
          "AvatarUrl":"http://saicommunity.wan.com/cfs-file/__key/system/images/anonymous.gif",
          "CoverPhotoUrl":null,
          "Birthday":null,
          "ContentId":"90d4de16-7595-4e8c-8146-ffbbfde27b92",
          "Url":"/members/testuser1",
          "Warnings":[
             
          ],
          "Errors":[
             
          ]
       },
       "CreatedUser":{
          "Id":2108,
          "IsSystemAccount":false,
          "Username":"testuser1",
          "DisplayName":"testuser1",
          "Password":null,
          "Points":1765,
          "QualityPercentile":14,
          "PrivateEmail":"test1@test1.com",
          "IsIgnored":null,
          "ForceLogin":null,
          "BanReason":"Other",
          "BannedUntil":"0001-01-01T00:00:00",
          "JoinDate":"2021-04-30T16:37:52.84",
          "LastVisitedDate":"2021-06-15T16:24:35.06",
          "LastLoginDate":"2021-06-15T14:59:48.16",
          "TermsOfServiceAcceptanceDate":null,
          "RequiresTermsOfServiceAcceptance":false,
          "AllowSiteToContact":false,
          "AllowSitePartnersToContact":false,
          "EnableEmail":true,
          "EnableHtmlEmail":true,
          "EnableTracking":true,
          "EnableConversationNotifications":true,
          "EnableCommentNotifications":true,
          "AccountStatus":"Approved",
          "EnableFavoriteSharing":true,
          "ConversationContactType":"FriendsOnly",
          "PostSortOrder":"Ascending",
          "EditorType":"",
          "EnableDisplayInMemberList":true,
          "EnableCollapsingPanels":true,
          "EnableDisplayName":true,
          "EnableUserSignatures":true,
          "HasPassword":true,
          "ModerationLevel":"Unmoderated",
          "RssFeeds":[
             
          ],
          "TotalPosts":73,
          "Presence":"Online",
          "EnablePresenceTracking":true,
          "TimeZone":5.5,
          "TimeZoneInfo":{
             "Id":"India Standard Time",
             "DisplayName":"(UTC+05:30) Chennai, Kolkata, Mumbai, New Delhi",
             "StandardName":"India Standard Time",
             "DaylightName":"India Daylight Time",
             "BaseUtcOffset":"05:30:00",
             "AdjustmentRules":null,
             "SupportsDaylightSavingTime":false
          },
          "TimeZoneId":"India Standard Time",
          "EnablePostPreviewPopup":false,
          "EnableEmoticons":true,
          "DateFormat":"d MMM yyyy",
          "ProfileFields":[
             {
                "Label":"AOL IM",
                "Value":"",
                "Warnings":[
                   
                ],
                "Errors":[
                   
                ]
             },
             {
                "Label":"Blog",
                "Value":"",
                "Warnings":[
                   
                ],
                "Errors":[
                   
                ]
             },
             {
                "Label":"Department",
                "Value":"",
                "Warnings":[
                   
                ],
                "Errors":[
                   
                ]
             },
             {
                "Label":"Education",
                "Value":"",
                "Warnings":[
                   
                ],
                "Errors":[
                   
                ]
             },
             {
                "Label":"Email",
                "Value":"",
                "Warnings":[
                   
                ],
                "Errors":[
                   
                ]
             },
             {
                "Label":"Facebook",
                "Value":"",
                "Warnings":[
                   
                ],
                "Errors":[
                   
                ]
             },
             {
                "Label":"Gallery",
                "Value":"",
                "Warnings":[
                   
                ],
                "Errors":[
                   
                ]
             },
             {
                "Label":"Google IM",
                "Value":"",
                "Warnings":[
                   
                ],
                "Errors":[
                   
                ]
             },
             {
                "Label":"Hobbies",
                "Value":"",
                "Warnings":[
                   
                ],
                "Errors":[
                   
                ]
             },
             {
                "Label":"Mobile",
                "Value":"",
                "Warnings":[
                   
                ],
                "Errors":[
                   
                ]
             },
             {
                "Label":"Occupation",
                "Value":"",
                "Warnings":[
                   
                ],
                "Errors":[
                   
                ]
             },
             {
                "Label":"Office Phone",
                "Value":"",
                "Warnings":[
                   
                ],
                "Errors":[
                   
                ]
             },
             {
                "Label":"Office",
                "Value":"",
                "Warnings":[
                   
                ],
                "Errors":[
                   
                ]
             },
             {
                "Label":"Title",
                "Value":"",
                "Warnings":[
                   
                ],
                "Errors":[
                   
                ]
             },
             {
                "Label":"Twitter",
                "Value":"",
                "Warnings":[
                   
                ],
                "Errors":[
                   
                ]
             },
             {
                "Label":"Windows Live IM",
                "Value":"",
                "Warnings":[
                   
                ],
                "Errors":[
                   
                ]
             },
             {
                "Label":"Yahoo IM",
                "Value":"",
                "Warnings":[
                   
                ],
                "Errors":[
                   
                ]
             },
             {
                "Label":"core_Birthday",
                "Value":"",
                "Warnings":[
                   
                ],
                "Errors":[
                   
                ]
             },
             {
                "Label":"core_Gender",
                "Value":"0",
                "Warnings":[
                   
                ],
                "Errors":[
                   
                ]
             },
             {
                "Label":"core_Public Email",
                "Value":"",
                "Warnings":[
                   
                ],
                "Errors":[
                   
                ]
             },
             {
                "Label":"core_Language",
                "Value":"en-US",
                "Warnings":[
                   
                ],
                "Errors":[
                   
                ]
             },
             {
                "Label":"core_Website",
                "Value":"",
                "Warnings":[
                   
                ],
                "Errors":[
                   
                ]
             },
             {
                "Label":"core_Location",
                "Value":"",
                "Warnings":[
                   
                ],
                "Errors":[
                   
                ]
             }
          ],
          "EnabledActivityMessageTypes":[
             "NewUser",
             "NewBlogPost",
             "NewForumThread",
             "NewMediaGalleryPost",
             "NewGroup",
             "NewGroupMember",
             "NewFriendship",
             "NewUserAvatar",
             "NewWikiPage",
             "UpdatedWikiPage",
             "ThirdPartyMessageType",
             "Status"
          ],
          "ExtendedAttributes":[
             
          ],
          "Language":"en-US",
          "Location":"",
          "PublicEmail":"",
          "WebUrl":"",
          "Signature":"",
          "SignatureFormatted":"<div style=\"clear:both;\"></div>",
          "Gender":"NotSet",
          "ProfileUrl":"/members/testuser1",
          "AvatarUrl":"http://saicommunity.wan.com/cfs-file/__key/system/images/anonymous.gif",
          "CoverPhotoUrl":null,
          "Birthday":null,
          "ContentId":"90d4de16-7595-4e8c-8146-ffbbfde27b92",
          "Url":"/members/testuser1",
          "Warnings":[
             
          ],
          "Errors":[
             
          ]
       },
       "File":null,
       "MetaKeywords":"",
       "MetaDescription":"",
       "MetaTitle":"",
       "OpenGraphTitle":"",
       "OpenGraphDescription":"",
       "OpenGraphImage":null,
       "CommentCount":0,
       "Tags":[
          
       ],
       "IsFeatured":false,
       "FeaturedImage":null,
       "SearchUniqueId":"cde538cf-ffc3-473f-ae38-50befe45ba27",
       "Application":{
          "ApplicationId":"c9115503-8d67-4147-adce-8a84fe6120e5",
          "ApplicationTypeId":"ca0e7c80-8686-4d2f-a5a8-63b9e212e922",
          "Url":"http://saicommunity.wan.com/new-custo/b/blog",
          "AvatarUrl":null,
          "Container":{
             "ContainerId":"b9d0eb55-b0d6-4eaf-bcb7-447f5efa7818",
             "ContainerTypeId":"23b05a61-c3e5-4451-90d9-bfa00453bce4",
             "Url":"http://saicommunity.wan.com/new-custo/",
             "AvatarUrl":"http://saicommunity.wan.com/cfs-file/__key/system/images/defaulthub.gif",
             "IsEnabled":true,
             "ParentContainer":{
                "ContainerId":"8dc0b606-bde2-422e-a790-0ac25ff47cd9",
                "ContainerTypeId":"23b05a61-c3e5-4451-90d9-bfa00453bce4",
                "Url":"http://saicommunity.wan.com/",
                "AvatarUrl":"http://saicommunity.wan.com/cfs-file/__key/system/images/defaulthub.gif",
                "IsEnabled":true,
                "ParentContainer":null,
                "Warnings":[
                   
                ],
                "Errors":[
                   
                ]
             },
             "Warnings":[
                
             ],
             "Errors":[
                
             ]
          },
          "IsEnabled":true,
          "Warnings":[
             
          ],
          "Errors":[
             
          ]
       },
       "ContentId":"cde538cf-ffc3-473f-ae38-50befe45ba27",
       "ExtendedAttributes":[
          {
             "Key":"FutureDatedPost",
             "Value":"Published",
             "Warnings":[
                
             ],
             "Errors":[
                
             ]
          },
          {
             "Key":"NotificationSent_BlogPostSubscribers",
             "Value":"True",
             "Warnings":[
                
             ],
             "Errors":[
                
             ]
          }
       ],
       "Warnings":[
          
       ],
       "Errors":[
          
       ]
    }

  • Is the file in PostImageFile the one you uploaded with the post? The two are different fields.

    Are you adding the file under "[New Post] > Write > Attach a file or URL" (below Title and Body), or under "[New Post] > Options > Post Image" ?

  • This was with the [New Post] > Write > Attach a file or URL" (below Title and Body), 

    You can find  "File": null in the response. 

  • How and when are you getting the above file properties? Is this from a REST call after the create has completed, and the File is null while the PostImage is not?

    I've discovered and logged an issue to investigate internally, related to accessing the various file properties of a blog post in AfterCreate events. One alternative is to handle CentralizedFileStorage.Events.AfterCreate to catch when a file is created. This would be separate logic from the rest of your BlogPost AfterCreate logic, and require parsing the path to confirm it's the file for the blog post in question.

    TE-16972: File/PostImageFile/OpenGraphImage are not available on BlogPost.AfterCreate event

Reply Children
No Data