Go Back   TechArena Community > Software > Tips & Tweaks
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read

Sponsored Links



Sharepoint Tips for adding items in List

Tips & Tweaks


Reply
 
Thread Tools Search this Thread
  #1  
Old 01-02-2011
Member
 
Join Date: May 2009
Posts: 258
Sharepoint Tips for adding items in List
  

The property ListsForCurrentUser class SPListCollection :

Here is a useful property to "play" with the lists of lists on a SharePoint site. The property ListsForCurrentUser, SPListCollection its class, the chance to decide whether or not to show only those listings where the current user has permissions (at least to display items). The default is set to True. If, in code, set this value to False, we would have the complete collection of SPList objects, that is, containing all lists of Sharepoint site chosen.
Code:
  SPWeb web = SPControl.GetContextWeb (this. Context);

  web.Lists.ListsForCurrentUser = true;
Add entries to a field of type choice of a Sharepoint list

Here's how to add, via code, the new values to a field of type choice (choice) of a sharepoint list:

Code:
 Site = new SPSite ("http://localhost");

  if (site! = null)

  {

  Site.OpenWeb SPList list = (). Lists ["ListName"];

  if (list! = null)

  {

  SPFieldChoice field = (SPFieldChoice) list.Fields ["FieldName"];

  if (field! = null)

  {

  field.Choices.Add ("new entry");

  field.Update ();

  list.Update ();

  }

  }

  }

Reply With Quote
  #2  
Old 01-02-2011
Member
 
Join Date: May 2009
Posts: 258
Re: Sharepoint Tips for adding items in List

Add attachments to an item in a list via code : Not long ago I've had to incorporate a number of new elements within a SharePoint list from a Console Application written in C #. Each element had one or more attachments on the file system into a temporary directory. To add these attachments to a single element, type SPListItem , we can use the Attachments property, this property is of type SPAttachmentCollection class that provides an Add () method just to add attachments. The method Add in question takes two parameters:
  • filename
  • an array of bytes that represent the actual file
Since then the path to a file you can use the method ReadFileBytes , I've just put into the category "USEFUL Methods", like this:
Code:
  static void Main (string [] args)

  {

  string fileName = "", filePath = "";

  try

  {

  SPSite site = new SPSite ("http:// <sitepath>");

  Sito.OpenWeb SPList list = (). Lists ["<listname>"];

  SPView view = list.DefaultView;

  Coll = SPListItemCollection lista.GetItems (view);

  SPListItem coll.Add item = ();

  item ["Title"] = "Item title";

  if (args [0]! = null & & args [0]! = "")

  {

  System.IO.Path.GetFileNameWithoutExtension fileName = (args [0]);

  System.Web.HttpServerUtility.HtmlEncode = fileName (fileName). Replace ("", "_");

  filePath = args [0];

  }

  item.Attachments.Add (fileName, ReadFileBytes (filePath));

  item.Update ();

  }

  catch (Exception err)

  {

  Console.WriteLine (err.Message);

  }

  Console.WriteLine ("Operation successful, press any key to exit");

  Console.Read ();

  }
The only thing they pay attention is the name of the file. In fact, if you are to handle files with names that contain spaces or, cmq, other "strange" characters (like hyphenated words), there is raised a nice exception. And because of what format the file name through the method HtmlEncode.
Reply With Quote
  #3  
Old 01-02-2011
Member
 
Join Date: May 2009
Posts: 258
Re: Sharepoint Tips for adding items in List

Create a Custom Content Type in code : Content types are one of the new Sharepoint Services 3.0, and they allow you to create a tree structure to manage the various types of content that can be used in SharePoint sites. So every different kind of list, from the custom document library, has at its base its content type. To create these objects, there is a user interface, accessible under Site Settings> Site content types. It may be useful, however, to create code to meet our needs to customize sharepoint. Let's see how:
Code:
  SPWeb web = new SPSSite ("http://localhost"). OpenWeb ();

  SPContentType tipoBase web.AvailableContentTypes = ["Item"];

  SPContentType myCT = new SPContentType (tipoBase, web.ContentTypes, contentTypeName);

  web.ContentTypes.Add (myCT);

  myCT.FieldLinks.Add (new SPFieldLink (web.AvailableFields ["Author"]));

  myCT.Update ();
In this example, we created a content type that inherits from the base type "Item", and we added the column "Author." This type of column is called "Site Column" column of the site are also part of the new features in the latest version of Sharepoint, and have the task of defining the actual content of the various content types. So how we can add to our content type, a column already exists within the site, so we can create for ourselves our own columns and put them in our content type.
Code:
  SPWeb web = new SPSSite ("http://localhost"). OpenWeb ();

  SPContentType tipoBase web.AvailableContentTypes = ["Item"];

  SPContentType myCT = new SPContentType (tipoBase, web.ContentTypes, contentTypeName);

  web.ContentTypes.Add (myCT);

  / / Add a column pre-existing 

 myCT.FieldLinks.Add (new SPFieldLink (web.AvailableFields ["Author"]));

  / / Create a custom column and add it to content type 

 web.Fields.AddLookup string fieldName = ("MyCustomField" web.Lists ["MyLookupList"]. ID, web.ID, true);

  SPFieldLookup web.Fields lookup = ["MyCustomField"] as SPFieldLookup;

  myCT.FieldLinks.Add (new SPFieldLink (web.AvailableFields [lookup.Title]));

  myCT.Update ();
Once created our own content type, we can assign one, or more, the listings on our site. That type of content, it was a very good move.

I hope this TIPS are Useful for you...
Reply With Quote
Reply

  TechArena Community > Software > Tips & Tweaks
Tags: , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Sharepoint Tips for adding items in List"
Thread Thread Starter Forum Replies Last Post
Adding SharePoint server on existing farm Gopal Shrivastav Networking & Security 4 28-04-2012 03:49 AM
Sharepoint - Bug lists with more than 1000 items and interruption of the inheritance of permissions Chillam Technology & Internet 4 01-03-2011 06:08 AM
Tips to collect all templates available on WSS and SharePoint Portal ramsun Tips & Tweaks 3 21-02-2011 05:45 AM
When searching in Windows 7 I get a list of group headers for items but not the items chyarte Operating Systems 3 17-01-2011 11:58 AM
Adding an Attachment to a List Item Programmatically in SharePoint bijayani Software Development 2 30-06-2010 12:06 PM


All times are GMT +5.5. The time now is 07:56 PM.