Posts Tagged document

SharePoint: Create folder hierarchy

Scenario:

We need to move a document from one document library to another document library with the condition that if folder hierarchy doesn’t exist, creates it.

Resolution:

Following code checks if folder hierarch exists or not. If exist, returns the reference of the folder, else create the hierarchy and return the reference of the newly created folder.

It takes three input. SPWeb, list name and folder path. Folder path should be like “/DocumentLibraryName/Folder1/Folder2/Folder3/Folder4”.

public SPFolder VerifyAndCreateFolder(SPWeb spWeb,string listName, string folderPath)

{

SPList spList = spWeb.Lists[listName];

string rootFolderUrl = spList.RootFolder.ServerRelativeUrl;

SPFolder spFolder = null;

if (spWeb.GetFolder(folderPath).Exists)

{

return spWeb.GetFolder(folderPath);

}

else

{

string destFolderPath = folderPath.Replace(“/” + listName + “/”,“”);

int lastIndexOfFowardSlash = destFolderPath.LastIndexOf(‘/’);

if (lastIndexOfFowardSlash < 0)

{

SPFolderCollection spFolderColl = spWeb.GetFolder(rootFolderUrl).SubFolders;

spFolder = spFolderColl.Add(destFolderPath);

return spFolder;

}

else

{

lastIndexOfFowardSlash = folderPath.LastIndexOf(‘/’);

string parentFolderURL = folderPath.Remove(lastIndexOfFowardSlash);

spFolder = VerifyAndCreateFolder(spWeb, listName, parentFolderURL);

SPFolderCollection spFolderColl = spWeb.GetFolder(rootFolderUrl).SubFolders;

spFolder = spFolderColl.Add(“/” + listName + “/” + destFolderPath);

return spFolder;

}

}

}

Advertisements

Comments (1)

SharePoint Document Upload Error: Unable to complete this operation. Please contact your administrator.

Scenario: 

When we try to upload document / images error page appear with following extension: 

Unable to complete this operation. Please contact your administrator. 

When we try to upload text document, it worked but when we increased the sixe from 45Bytes to 450KB, it failed with the same message. 

Resolution: 

You ran out of space either on SQL Server or on Server Machine. Created space and we were back on track.

Comments (5)

MOSS 2007: MS Office Word 2003 document error

Scenario:

 

When of our client tried to open document using MS Office 2003 from SharePoint 2007, following error displayed:

 

“File is locked. In order to save changes to the file, you can other close the file before making any changes and then re-open it, use Save As (File menu) to save the file using another file name, or turn off Shared Workbook and then save the fie.”

 

 

 

 

 

 

 

Resolution:

 

Rename the file and it will work fine. We tried some other options but things do not seems to be working.

 

Leave a Comment

Upload file in MOSS / SharePoint using code

It is easy and simple to upload a file in the document library of MOSS / SharePoint. The code for upload the file in C# is as under: 

SPSite sp = new SPSite(“URL of the site collection”);     

SPWeb site = sp.OpenWeb();      

SPFolder folder = site.GetFolder(“Document Library Name”);     

SPFileCollection files = folder.Files; FileStream fStream = File.OpenRead(“C:\\upload.doc”); //path of the file to upload     

byte[] contents = new byte[fStream.Length];     

fStream.Read(contents, 0, (int)fStream.Length);     

fStream.Close();      

Hashtable MetaDataTable = new Hashtable();     

MetaDataTable.Add(“Comments”, “Hello World”);      

SPFile currentFile = files.Add(“URL of the document library/upload.doc”, contents, MetaDataTable, true); 

You can addd the meta data as well by using Hash Table. We have populated the “Comments” column with “Hello World”. 

With or without using the following code

 SPListItem doc = currentFile.Item;  

We can do a lot of things with the uploaded file.

For more information, visit the Microsoft link. It provides more information about the error checking and other.

Comments (20)