Results 1 to 3 of 3

Thread: Sharepoint Tips for adding items in List

  1. #1
    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 ();
    
      }
    
      }
    
      }

  2. #2
    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.

  3. #3
    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...

Similar Threads

  1. Adding SharePoint server on existing farm
    By Gopal Shrivastav in forum Networking & Security
    Replies: 4
    Last Post: 28-04-2012, 03:49 AM
  2. Replies: 4
    Last Post: 01-03-2011, 06:08 AM
  3. Replies: 3
    Last Post: 21-02-2011, 05:45 AM
  4. Replies: 3
    Last Post: 17-01-2011, 11:58 AM
  5. Adding an Attachment to a List Item Programmatically in SharePoint
    By bijayani in forum Software Development
    Replies: 2
    Last Post: 30-06-2010, 12:06 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •