|
| ||||||||||
| Tags: add attachment, class splistcollection, list, sharepoint, spweb |
![]() |
| | Thread Tools | Search this Thread |
|
#1
| |||
| |||
| Sharepoint Tips for adding items in List
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; 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
| |||
| |||
| 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:
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 ();
} |
|
#3
| |||
| |||
| 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 (); 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 (); I hope this TIPS are Useful for you... |
![]() |
|
| Thread Tools | Search this Thread |
| |
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 |