Unable to cast object of type 'Microsoft.SharePoint.SPListItem' to type 'Microsoft.SharePoint.SPFile
-
02-10-2020 - |
Domanda
In code below, at mentioned line I am getting this error: How I fix this?
SPList docLib = WSweb.GetList(WSweb.ServerRelativeUrl + "/Documents");
//SPList docLib = WSweb.Lists.TryGetList("Documents");
if (docLib != null) {
SPDocumentLibrary docs = (SPDocumentLibrary) docLib;
SPQuery checkedOutDocs = new SPQuery();
checkedOutDocs.Query = @"<Where><IsNotNull>
<FieldRef Name='CheckoutUser' />
</IsNotNull></Where>
<QueryOptions>
<ViewAttributes Scope='RecursiveAll' />
</QueryOptions>";
checkedOutDocs.ViewFields = "<FieldRef Name='ID' />";
checkedOutDocs.ViewFieldsOnly = true;
SPListItemCollection items = docs.GetItems(checkedOutDocs);
foreach(SPFile file in items) // At this line i am getting error
{
if (docmanager.getFileExtension(file.Item) == ".pdf") {
if (file.LockId != null) {
file.CheckIn("Checked in programatically!");
}
}
}
}
Soluzione
SPListItemCollection
is a collection of SPListItem
. Get File
object from SPListItem
. Example:
foreach (SPListItem item in items)
{
SPFile file = item.File;
}
Altri suggerimenti
Instead of using SPFile object use the SPListItem in foreach loop. And within the loop we can use the File property of SPListItem to get the SPFile object.
Try the below code...
foreach (SPListItem item in items){
SPFile file = item.File;
if(docmanager.getFileExtension(item) == ".pdf"){
if (file.LockId != null){
file.CheckIn("Checked in programatically!");
}
}
}
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a sharepoint.stackexchange