Sandeep's Blog: This blog is no longer updated, please visit http://sandeepnakarmi.com.np for latest posts

January 11, 2009

SPFile.Versions.GetVersionFromId or SPFile.Versions.GetVersionFromLabel might not return the desired version althought it exist

Filed under: Sharepoint 2007 — Tags: — sandeep nakarmi @ 9:35 AM

SPFile.Versions.GetVersionFromId or SPFile.Versions.GetVersionFromLabel might not return the desired version of a file. But when you iterate through all the versions and then check for versionId, then we get the desired SPFileVersion which is not null. So apparently, this is a bug in the API.


//this might not work all the times as it wil fail to retrieve some of the versions of a file
//especially with  Published major version when there are minor versions also present. 
SPFile file=web.GetFile(itemUrl);
SPFileVersion fileVersion=file.Versions.GetVersionFromId(1024) //Here the fileVersion may be null

//where as this works fine
SPFile file=web.GetFile(itemUrl);
SPFileVersion fileVersion=null;
foreach(SPFileVersion ver in file.Versions)
{
    if(ver.ID==1024)
    {
       fileVersion=ver;
       break;
    }
}

SPFile.OpenBinaryStream() might throw exception when getting file by GUID and immediately calling OpenBinaryStream

Filed under: Sharepoint 2007 — Tags: — sandeep nakarmi @ 9:17 AM

Calling OpenBinaryStream throws COM exception – 0×80070092

 

Create an instance of SPFile using GetFile method and pass the GUID of the file in its parameter. Then immediately call OpenBinaryStream method in the instance of spfile. It then throws a COM exception “The path specified is being used in a substitute. (Exception from HRESULT: 0×80070092)”

 


SPFile oSPFile = OSPWeb.GetFile("GUID of the file");<span style="font-family:Consolas;font-size:small;"> </span>

Stream oStream = oSPFile.OpenBinaryStream();

But, at the same time, the below mentioned code works fine. 


SPFile oSPFile = OSPWeb.GetFile("GUID of the file");
string strName = OSPFile.Name;
Stream oStream = oSPFile.OpenBinaryStream();
//Or even, this code works.
SPFile oSPFile = OSPWeb.GetFile("URL");
Stream oStream = oSPFile.OpenBinaryStream();

 

There is a method in the SPFile class called GetFileStream. This method calls the COM level method GetFileAsStream which accepts webRelativeUrl as one of the parameters. When we access a property of the file before calling the OpenStream method this method gets populated with the relative path of the file we are working with. If we directly call the method OpenBinaryStream right after we get the file though the GUID, this parameter is null and will fail with the exception:”The path specified is being used in a substitute”. 

 

To work around this problem, use the GetFile(String) http://msdn.microsoft.com/en-us/library/ms476063.aspx  version of the GetFile method and rather than GUID, pass in the URL to the file.

December 18, 2008

WSS Field Names for Lists & Document Libraries

Filed under: Sharepoint 2007 — Tags: — sandeep nakarmi @ 5:58 AM

For a list of field names and its internal names in MOSS ans WSS refer to the link below:

http://blog.thekid.me.uk/archive/2007/03/21/wss-field-display-amp-internal-names-for-lists-amp-document-libraries.aspx

Theme: Shocking Blue Green. Blog at WordPress.com.

Follow

Get every new post delivered to your Inbox.