HiddenNetwork.com Banner
Showing posts with label General SharePoint (All Versions). Show all posts
Showing posts with label General SharePoint (All Versions). Show all posts

Monday, June 04, 2007

Why isn't there a default value for lookup fields?

Oh, lookup fields, how I hate you.
I just realized that it is not possible in the user inteface to set a default value for a lookup field. Is it that hard to do an interface that will show the user creating the field the options from the looked-up list and allow him to select a default value?
Apperantly it is. I guess Microsoft were worried about what happens if someone deletes the looked-up value.

Not content with my findings, I tried looking in the object model and discovered that you can set up a default value for a lookup field using the following code:

SPList docs = w.Lists["Documents"];
SPFieldLookup fl = (SPFieldLookup)docs.Fields["City"];
fl.DefaultValue = "1;#Mexico City";
fl.Update();

I will add this option to my Utility Pack when possible.

Using the above code solves the issue almost totaly. When you use the web interface to add a list item or upload a document, the default value comes up in the lookup box. However (and this is the big one), from the office applications (both 2003 and 2007) the default value is ignored and the lookup box always begins at the "blank" option.

Should I waste one of my MSDN support calls on this and ask MS for a fix? or should I just learn to live with it?

Wednesday, May 30, 2007

Another reason for "the virtual server that is referenced here is not in the config database."

If you get the error "the virtual server that is referenced here is not in the config database" when you try to use object model, and if the microsoft KB article didn't solve it for you, and you are totaly puzzled...you may have the same problem I have - lack of attention.
This error appeared on my screen because I was developing on a server that has both sharepoint 2003 and sharepoint 2007, and in my project I referanced (without noticing) the DLL for WSS2 and not WSS3.
So ofcourse the code would throw an error it can't find the virtual server in the configuration database! it was looking in the wrong configuration database!
HA!, what a silly person I am...wasted half a day trying to troubleshoot this - going into the database, rebuilding the sites, rebuilding the application, extending the sites, mucking with IIS...untill I noticed the path to the DLL has "60" in it.
BAH!

Sunday, May 27, 2007

On my dashboard

Just a couple of valuable links to pages I always print out and hang in front of me when I am in a new cubical - a list of SharePoint URLs to get to commonly used administrative functions on a SharePoint site:
SharePoint 2003 URL Quick Links and SharePoint 2007 URL Quick List.

Thanks as always to Heather Solomon.

Monday, April 30, 2007

One of more field types are not installed properly

Another FAQ that I see a lot from developers - they get the error "One of more field types are not installed properly" in their applications.
No, don't blame your database in being corrupt, and don't go beat up the guy who create the list and the fields. The problem is simple:
You'r code is doing a CAML query, and uses a field name that does not exist.

I hear you say "but I checked my CAML query, and the field names are correct", and I have to remind you - field names in CAML queries should be internal names, not display names.
This is why, if you have a field called "Parent Task" (for example), in CAML you should refer to it as "Parent_x0020_Task", because the internal name is encoded to avoid special characters.
Also, the internal name may be totaly different from the display name - this will happen if you created a field and then renamed it. So the internal name remains the same as the old name, but the display name changed.
Another scenario for this is when you created your own list definition, and specified different internal and display names. (this happened to me recently)

So, how to know what is the internal name for your field? simple - just click on it!
Let me elaborate - create a view in the list that has that field, and click on the field to sort the list by it. In the web address (url) of the page you are directed to, the internal name of the field should be displayed, after the query string "SortField=".
If the field does not allow sorting, you can go to the list settings and click on the field, as if you want to change it's settings. Again, the internal name of the field will be in the URL address of the page.
Please note, that the field names in the URLs are encoded again, and you will have to compensate for that. for example, a field with a space like "Parent Task" will not be displayed like I said before in the address bar as "Parent_x0020_Task", but instead as "Parent%5fx0020%5fTask" (the underscore character was replaced by the unicode equivalent "%5f").

So you want an easier way to find the internal name (without coding)?
Just do yourself a huge favour and download U2U CAML Builder which is a terrific application to help you build CAML queries.
Since the tool knows it has to use internal names, it just does it for you!.
I cannot live without this tool
U2U CAML Builder screen

Explanation for sharepoint begginers - the difference between web parts and lists (my two web parts are showing the same data)

Did you try adding another web part to a page and it displays the same data as another web part on the page? Here is the answer.

As part of my habit of posting in this site an answer for any question I see more than once in the forums, I want to share with you an explanation I use in the forums to let sharepoint begginers realize what is the difference between a web part and a list.

The confusion sounds like this:
"I'm a newbie to the SharePoint world so if this is a really amateur question, you know why. I am currently tasked with setting up the SharePoint site for my department. I have tried to put two instances of the Links web part on the top level of my site. It will allow me to do this but when I make a change to one of the web parts the change also occurs in the other Links web part." (taken from an msdn forum post)

To which I reply:
You are confusing "web parts" and "lists"
A "web part" is a mechanism to display data, while a "list" is like a small database - a mechanism to store data.

What you did, is have one list (links, or contacts) and two web part displaying the same list - same data.

If you want two different sets of data then the easy way would be to create another list, and add a web part to look into that list.

Another option is to create a field in the list by the name (for example) "show in web part 1" of type boolean, and then configure the two webparts that show the same information to show based on that field (this method is known as a filter).

Wednesday, January 17, 2007

Call for Microsoft - SharePoint as Open Source!

In one of the most interesting articles I've read lately, Stephen Walli is explaining why and how sharepoint should be made open source.
It is very interesting and I wonder if someone from Microsoft will read this and say "hmmm...let's do it!".
I really hope so, because I can think of so many changes I would like to do to the sealed classes, and I can only imagine what the hard core developers would do. Next time we find a bug, we can fix it ourselves and not have to wait in queue for the service pack, while our customer is canceling the project.
Microsoft will gain more developers living sharepoint inside-out and more and more integrating products will come out.
What do you think?

Sunday, November 26, 2006

Search Highlighting For MOSS2007 (and 2003) - Update

This is an update to a year old post about a product that I helped write - the SharePoint Search String Highlighter. Apperantly my friends in KwizCom have added support to MOSS2007!

Before you say "but moss has search highlighting", let me remind you that this solution also highlights in the documents (word and pdf), and can also help you solve a problem that is usual with search results - make sure the links open in word and not in the browser (I hate when that happens).

So take a look - tell me what you think...

Thursday, November 16, 2006

Happens every time - adding a web part class to a project

I hate it when I dont learn from my bad expirience...

When I have a solution with a web part and I want to add another web part to the same project I just right click the project, select "Add" and choose "Class". Then I tell the class to inherit from "WebPart" and proceed to program my code.

And when I get to deploying it - the new class isnt shown in the new webparts gallery!

why?

because I forget every time to mark the class as "public" - when Visual Studio creates a new class in a project, the default is just "class newwebpart".

So the lesson for today - dont beat yourself going over the web.config file - just make sure the class is marked public.

Tuesday, August 29, 2006

Common (and simple) coding tasks in sharepoint

Well, everyone went to teched last week and I was left at work stuck with a project with a strict deadline. So out of boredom I decided that I will compile a small snippet database for beggining sharepoint developers.
I see a lot of people in the microsoft public sharepoint developer forums asking these questions over and over again, so I thought I will save everyone some time by answering these right here.
Some people may say these are RTFM questions, since the SDK contains all of these examples, but apperantly people dont bother with the SDK, or find it confusing to navigate through. So here is my 2 cents.

The purpose here is to give some code samples to common tasks like:

  • getting a reference to a site
  • Iterating over all lists in a site
  • getting a reference to a list
  • getting a reference to an item in a list
  • getting a reference to the item's properties
  • getting a reference to a document and its properties
  • adding a new item to a list
  • modifying an item in a list

If you feel I should add to this list, let me know and I will write some sample code. However, please remember this is for simple, common tasks and I wont start giving application samples here.

So, lets get down to it!

Getting a reference to a site

Ok, you have a sharepoint site in a URL "http://server/sites/site" and you want to get the object of the site from its URL. What do you do?

using(SPSite mySite = new SPSite("http://server/sites/site"))
{
   using(SPWeb myWeb = mySite.OpenWeb())
   {
   }
}

Now you have the SPWeb object, allowing you to get information about the site you used in the URL. For example, iterating all lists in the site:

using(SPSite mySite = new SPSite("http://server/sites/site"))
{
   using(SPWeb myWeb = mySite.OpenWeb())
   {
      string linksHtml = "";
      foreach(SPList list in myWeb.Lists)
      {           
         string listLink = "<a href='" + list.DefaultView.Url.ToString()          + "'>" + list.Title + "</a>(<b>"+list.Items.Count+"</b>)<br>";
         linksHtml += listLink;
      }
   }
}

The above example also showed how to get a reference to a list by iterating over the lists in a site. But what if you want a specific list called "contacts"? The following will show you how to get the object for a list that you know the name of:

using(SPSite mySite = new SPSite("http://server/sites/site"))
{
   using(SPWeb myWeb = mySite.OpenWeb())
   {
      SPList contactsList = myWeb.Lists["Contacts"];
   }
}

Now lets iterate through the items in the list and get item's properties:

using(SPSite mySite = new SPSite("http://server/sites/site"))
{
   using(SPWeb myWeb = mySite.OpenWeb())
   {
      SPList contactsList = myWeb.Lists["Contacts"];
      foreach (SPListItem contact in contactsList)
      {
         string contactLastName = contact["Last Name"].ToString();
      }
   }
}

Now some of you are saying - "what about documents?", to which I answer that document libraries are the same as lists (ok, there are some differences but we will leave that for future articles). To get to a document in a document library you can either use the code above to iterate through the library and its files, or, if you know the file's URL, you can do this:

using(SPSite mySite = new SPSite("http://server/sites/site"))
{
using(SPWeb myWeb = mySite.OpenWeb())
{
SPFile file = myWeb.GetFile("http://server/sites/site/library/folder/file");
}
}

File properties are available through its Item property. If you have for example a text field in the document library called "My Custom String Property", and you want to know the value for that field in a specific file, use the following code:

using(SPSite mySite = new SPSite("http://server/sites/site"))
{
using(SPWeb myWeb = mySite.OpenWeb())
{
SPFile file = myWeb.GetFile("http://server/sites/site/library/folder/file");
string filePropertyValue = file.Item["My Custom String Property"].ToString();
}
}

We now want to add a new item to a list:

using(SPSite mySite = new SPSite("http://server/sites/site"))
{
using(SPWeb myWeb = mySite.OpenWeb())
{
SPList contactsList = myWeb.Lists["Contacts"];
SPListItem newItem = contactsList.Items.Add();
newItem["First Name"] = "Ishai";
newItem["Last Name"] = "Sagi";
newItem.Update();
}
}

Lets change all items in the list to build the email address based on the first and last name (in the format of 'first.last@testing.com') :

using(SPSite mySite = new SPSite("http://server/sites/site"))
{
using(SPWeb myWeb = mySite.OpenWeb())
{
SPList contactsList = myWeb.Lists["Contacts"];
foreach(SPListItem existingItem in contactsList.Items)
{
existingItem["E-mail Address"] = existingItem["First Name"] + "." + existingItem["Last Name"] + "@testing.com";
newItem.Update();
}
}
}

Well, that's it. If you think of more snippets like that, let me know!

[Update 4/4/2007 : I have replaced the old samples to best practice format (using using)]

Sunday, July 09, 2006

FAQ - how to force documents to open in their own application and not IE

Now, here is a FAQ if I ever heard one! (this post is more for me - because every time I am questioned about this, I say that I remember there was a solution, but I have to research for it every time)

Almost every one I help implement sharepoint asks me how to make sure that when his users click a link to a document it will open in word and not in IE. Same for excel and powerpoint.

Well, The good news are that in MOSS 2007 its easy - just go to a document library's advanced settings and you will see that the default it to "open in the client application".

Now, this is great, but still we have two more problems:

  1. What about links to documents that are not in a document library
  2. What about sharepoint 2003?

The answer is easy. The settings on how to open a document can be changed on the client side. The article in the technet (http://support.microsoft.com/?scid=kb;en-us;162059) shows how to change the client machine preferance on how to deal with each application.

Hope this saved you some research... if it did, please kick it on SharePointKicks.com

Wednesday, July 05, 2006

New in this blog - a blog roll!

I have just registered to BlogRolling.com and created my sharepoint blog roll. This is a list of blogs I regularily visit, and I want to share them with you guys.

You can see the blog roll on the right hand side of this page, on the bottom. There is also a RSS link so you can set your rss reader to tell you when I find a new blog or sharepoint site. Hope you like it!

Sharepoint Kicks - a new site for sharepoint information

The idea behind this new site is:

"sharepointkicks.com is a community based news site edited by our members. It specializes in Microsoft SharePoint technologies, including web parts, tools, development, configuration, customization, and use of Microsoft SharePoint Server and Windows SharePoint Services.

Individual users of the site submit and review stories, the most popular of which make it to the homepage. Users are encouraged to 'kick' stories that they would like to appear on the homepage. If a story receives enough kicks, it will be promoted."

I have added some of my articles to the "kick queue" and I hope my readers (thats you!) will take the time to click the new "kick me" images to vote for me!

Remember, a kick is a good thing!

http://www.sharepointkicks.com/

Monday, February 06, 2006

Alerts not working just for one user? maybe its spam...

Hey, I was just reminded in the msd2d.com discussion groups about an issue I had with a customer about a two years ago. They had one user who never got alerts. All the other users got the alerts with no problems, but this one user got nothing. He was able to sign to the alert, when checked in the database you saw that he was signed to the alert, but when the list would change - other user got the email and he didn't! I will spare you the whole story (took us a month to chase it down). It turned out the outlook 2003 was treating sharepoint emails as just mail. Why? I cant tell. Perhaps the user was annoyed by a different alert, set junk on it and forgot about it... Hope your users are more careful!

Wednesday, January 04, 2006

SharePoint in a Stand Alone Server Gives Error On Being offline

Problem Description:

Well, this is an old one, but I always forget about it when I encounter it. When you use a stand alone SharePoint server (for example in a virtual machine like Microsoft VPC or VMware) to develop or test something, not all of the features work. For example:

  • DataSheet view will not update rows in a list or document library and will show you an error stating it is offline. "Cannot connect to the server at this time. Changes to your data cannot be saved."
  • Office task pane that is supposed to show you "document updates" and "Shared workspace", is also telling you you are offline. "You are currently working offline. Check your network connection and then click get updates."

Cause:

The reason for this behavior is that the office objects that connects office to sharepoint are checking the network status and they see that you don't have a network (since you are on a standalone server).

Solution:

Well, one way would be to setup another machine and connect the two, or maybe set up a loopback adapter.

But the easy way to solve this is to just stop the network sensing mechanism and fooling office! Here is how you do it:

  1. Close all office applications and internet explorer windows.
  2. Click Start>Run
  3. write in the text box "net stop sens" and press enter.

that's it! Now the datasheet view will update items and office-sharepoint integration will work smoothly!