[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1557
  • Last Modified:

Using MongoDB with Sharepoint

I built a web page in VS 2012 that connects to a mongodb and returns records to a gridview. It works well. I am trying to recreate that page within SharePoint. I basically cut and pasted the code from the working page into the SharePoint site and then added the references to the mogodb drivers. The page compiles and builds without errors. When I go to run the page and click the search button to return the records from the mongodb, I get the error: Could not load the file or assembly 'MongoDB.Driver. . .' The system could not fins the file specified. Like I said, it works fine in the first web application, but not in SharePoint. I am completely new to SharePoint. Is there something I am missing with regards to references, or something else like that? Any ideas would be appreciated. It is SharePoint 2013, building the pages in VS 2012.
0
dodgerfan
Asked:
dodgerfan
  • 3
  • 3
1 Solution
 
Jamie McAllister MVPArchitectCommented:
When you say you cut and pasted the working code into SharePoint, how did you do that? Into page content?

Coded solutions are deployed to SharePoint using the solutions framework. A specific way to package and deploy solutions. I'm guessing that wasn't used here. Depending on your original solution you have have to refactor somewhat to get it working in SP.

Could you paste the code that references the MongoDB drivers and give more details about how you've added this to SharePoint?
0
 
dodgerfanAuthor Commented:
I created a new SharePoint empty application with VS 2012. I then created a new application page. I added the mongodb references to the project and added the using statements to the code behind page. I then copied the code from the regular web application into the new application page in sharepoint. The code works in the web app, but not in the SharePoint project. The code compiles and runs fine, but when you click the GetData button, the onclick event give the error. That onclick event is the code that connects to and uses mongodb. The code is below. Like I mentioned, SharePoint is new to me. This is the first time I have tried any development work in it.
I have these references in the code. I get this error: 'Could not load the file or assembly 'MongoDB.Driver. . .' The system could not fins the file specified'

using MongoDB.Bson;
using MongoDB.Driver;
using MongoDB.Driver.Builders;

protected void ConnectMongoDB()
{
var connectionString = "mongodb://test";
var client = new MongoClient(connectionString);
var server = client.GetServer();
var database = server.GetDatabase("db");
var collection = database.GetColiection<LazyBsonDocument>("entity_map");
var inserted = tbSSN.Text.ToStringO;
var q = Query.Matches("SSN.data", "^" + inserted + ".*");
 
String[] String Names = new String[] { "COMP", "FIRST_NAME", "LAST_NAME", "MIDDLE_NAME", "SSN" };
List<DataFromDB> mdfdblist = new List<DataFromDB>();
foreach (var doc in collection.Find(q)) {
DataFromDB mdfdb = new DataFromDBO;
using (doc)
{
foreach (var stringName in StringNames)
{
if (doc.Contains(stringName)) {
var data = doc[stringName];
foreach (var subDoc in data.AsBsonArray)
{
string value = subDoc["data"].AsString; switch (stringName)
{
case "COMP": mdfdb.COMP = value;
break;
case "FIRST NAME": mdfdb.FIRST _NAME = value;
break;
case "LAST NAME": mdfdb.LAST _NAME = value;
break;
case "MIDDLE_NAME": mdfdb.MIDDLE_NAME = value; break;
case "SSN":
mdfdb.SSN = value;
break;
}
}
}
}
}
mdfdblist.Add(mdfdb );
}
grdResults.DataSource = mdfdblist;
grdResults.DataBind();
}
0
 
Jamie McAllister MVPArchitectCommented:
Were the driver files included in the solution and deployed to the server at the same time as the code?

I suspect the code is fine, and as the error message states it can't find the driver files (because they haven't been deployed to SharePoint).
0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
dodgerfanAuthor Commented:
How can I deploy the driver files to sharepoint?
0
 
Jamie McAllister MVPArchitectCommented:
0
 
dodgerfanAuthor Commented:
Yep, it seems SharePoint needs the referenced .dll's in it's own bin folder. I copied them there and suddenly it works. Thanks for the help in troubleshooting this.
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now