I have a List View web part (In the page > Add web part > App > document library name). I need to update the TitleUrl property of this web part through C# code.
The SaveChanges() method below works for my visual web parts, but not for the List view webpart. I get an error saying: Exception has been thrown by the target of an invocation
webpartManager = myPage.GetLimitedWebPartManager(System.Web.UI.WebControls.WebParts.PersonalizationScope.Shared);
webPart.TitleUrl = titleURL;
webpartManager.SaveChanges(webPart);
I did a casting as below
XsltListViewWebPart listViewWebPart = (XsltListViewWebPart)webPart;
listViewWebPart.TitleUrl = titleURL;
webpartManager.SaveChanges(listViewWebPart);
But I get the error at SaveChanges() method.
An exception of type 'System.Reflection.TargetInvocationException' occurred in Microsoft.SharePoint.dll but was not handled in user code
The error description is as below
> at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[]
> arguments, Signature sig, Boolean constructor) at
> System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj,
> Object[] parameters, Object[] arguments) at
> System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags
> invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
> at System.Reflection.RuntimePropertyInfo.GetValue(Object obj, Object[]
> index) at
> Microsoft.SharePoint.WebPartPages.BinaryWebPartSerializer.DoesPersonalizedPropertValueMatchDefaultValue(SPPersonalizablePropertyEntry
> spPersonalizablePropertyEntry, Object value, Control defaultControl)
> at
> Microsoft.SharePoint.WebPartPages.BinaryWebPartSerializer.Serialize(PersonalizationScope
> scope, BinaryWebPartSerializerFlag binaryWebPartSerializerFlags,
> BinaryWebPartSerializerWriter writer) at
> Microsoft.SharePoint.WebPartPages.BinaryWebPartSerializer.Serialize(SerializationMode
> mode, BinaryWebPartSerializerFlag binaryWebPartSerializerFlags,
> SPSerializationBinderBase serializationBinder,
> BinaryWebPartSerializerWriter writer) at
> Microsoft.SharePoint.WebPartPages.BinaryWebPartSerializer.Serialize(SerializationMode
> mode, BinaryWebPartSerializerFlag binaryWebPartSerializerFlags,
> SPSerializationBinderBase serializationBinder) at
> Microsoft.SharePoint.WebPartPages.SPWebPartManager.SaveChangesCore(SPLayoutProperties
> layoutProperties, Boolean httpGet, Boolean saveCompressed, Boolean
> skipRightsCheck, Boolean skipSafeAgainstScriptCheck, WebPartTypeInfo&
> newTypeId, Byte[]& newAllUsersProperties, Byte[]&
> newPerUserProperties, String[]& newLinks) at
> Microsoft.SharePoint.WebPartPages.SPWebPartManager.SaveChangesInternal(SPLayoutProperties
> layoutProperties, Boolean skipRightsCheck, Boolean
> skipSafeAgainstScriptCheck) at
> Microsoft.SharePoint.WebPartPages.SPWebPartManager.SaveChanges(Guid
> storageKey) at
> Microsoft.SharePoint.WebPartPages.SPLimitedWebPartManager.SaveChanges(WebPart
> webPart) at
> WSP.ABC.ABC.ABCWindow.<>c__DisplayClass5.<ChangeWebPartProperties>b__4()
> at
> Microsoft.SharePoint.SPSecurity.<>c__DisplayClass5.<RunWithElevatedPrivileges>b__3()
> at
> Microsoft.SharePoint.Utilities.SecurityContext.RunAsProcess(CodeToRunElevated
> secureCode)
any reason why ?
ListViewWebPart wp = (ListViewWebPart)webpart;