Upload an Image to your SharePoint Site
24. April 2007 05:49

It's very easy to add a file/image to your SharePoint site via the Object Model.

 

            System.IO.MemoryStream str = new System.IO.MemoryStream();
SPSite site = new SPSite(Page.Request.Url.ToString());
SPWeb web = site.OpenWeb();

Bitmap.FromFile("Source URL").Save(str,Imaging.ImageFormat.Gif);
web.AllowUnsafeUpdates = true;

web.Folders["PublishingImages"].Files.Add("filename",str);
web.Folders["PublishingImages"].Update();

web.Close();
site.Close();

I was also interested in adding an image to an image library via the OM, but haven't had a chance to play around with it yet, let me know if you've done this already.

Tags: Comments (0) | Permalink
Enterprise Library works great with SharePoint 2007
19. April 2007 09:37

I bet alot of you are sitting at your desk today, coding away on your MOSS Webpart, and thinking to yourself "Geez, I really hate coding all this data access stuff, isn't there a better way!?"

Yes! The Enterprise Library 3.0 - April 2007 release.

So you need to grab some sql data and shove it in a dataset huh?

myDataset = CreateDatabase("DBName").ExecuteDataSet("SPName", params);

Seriously, thats it! One line! Don't even create a connection!


It works with Oracle too... Of course you can do about a million equally amazing things with the Enterprise Library, so why not use it with your SharePoint development starting right now?


So you just want to make a connection to a database (an external database, NOT your SharePoint database!). Open the configuration tool, load your sharepoint web.config, and add your connection string.



Add two references to your web part project:



  1. Microsoft.Practices.EnterpriseLibrary.Data
  2. Microsoft.Practices.EnterpriseLibrary.Common

But you can't just add these from anywhere... For code security reasons, Sharepoint is not going to let you use just any old DLL files, you've got to use Strong Named Assemblies. Now I know your thinking "Geez, I have to go add a key to all those projects and recompile" .... But the Standards and Practices team at Microsoft is one step ahead of you in this release. If you go to the installation directory for Enterprise Library, you'll find a batch of pre-compiled, pre-signed, dll files!


Make your life a little easier and add a using statement like this to your class:

using ENT = Microsoft.Practices.EnterpriseLibrary.Data;

Bet you didn't know you could assign an alias to a namespace? Well things inside EnterpriseLibrary.Data can get confused with System.Data, so it's best we alias the Enterprise Library with something short and sweet, like "ENT".


Ok, now have fun using things that begin with:

ENT.DatabaseFactory.CreateDatabase("DBName")....

Where 'DBName' is the name you associated with your connection string using the configuration tool. Enterprise Library will go out, look it up, open the connection, and execute your commands, all the while cleaning up nicely after itself.

(Remember during deployment you'll need to take care of safe control entries and code access security for all the assemblies.)

There are piles of useful configuration, data, and logging tools inside the application blocks, take a look at the documentation and see what will work for you.

Tags: Comments (9) | Permalink
Check If User is Member of Group
19. April 2007 08:47
Does your webpart need to behave differently depending on a user's membership in a given group? This is actually extremely easy to check using the Object Model.
        private bool IsMember()
{
bool isMember;
SPSite site = new SPSite(SiteURL);
SPWeb web = site.OpenWeb();
isMember = web.IsCurrentUserMemberOfGroup(web.Groups["GroupName"].ID);
web.Close();
site.Close();
return isMember;
}
Tags: Comments (6) | Permalink
BDC Crawl Error - The Parameter is Incorrect
17. April 2007 06:40
Working on a fresh local install of MOSS I ran into this error today while trying to crawl a BDC data source:


The parameter is incorrect. (The Local ServerFarm cannot be detected. Your access may be invalid.)


I had been having some real trouble with my SSP earlier in that day, and my first thought was that I was going to have to create a new SSP, but then I realized the issue was actually something embarrassingly simple.




Check your default content access account, and the account your Office Sharepoint Server Search service is running under. Local Service is not sufficient!!!



I hope this helps someone else!
Tags: Comments (0) | Permalink